当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Two list view inside a ViewPager, only one is showing

问题描述:

I currently have a ViewPager with three fragments as pages. The first two pages display fragments with custom list views which are built from RSS feeds using AsyncTask. However, when the app runs, only one list view is displayed, the other page is just blank. (Note: I'm using ViewPagerIndicator in this project).

Main activity:

public class ViewPagerExample extends FragmentActivity {

private static final String[] TITLETAB = new String[] { "Latest", "New", "Misc" };

private MyAdapter mAdapter;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.simple_tabs);

mAdapter = new MyAdapter(getSupportFragmentManager());

mPager = (ViewPager)findViewById(R.id.pager);

mPager.setAdapter(mAdapter);

mIndicator = (TabPageIndicator)findViewById(R.id.indicator);

mIndicator.setViewPager(mPager);

mIndicator.setCurrentItem(1);

}

public static class MyAdapter extends FragmentPagerAdapter {

public MyAdapter(FragmentManager fm) {

super(fm);

}

@Override

public int getCount() {

return ViewPagerExample.TITLETAB.length;

}

@Override

public Fragment getItem(int position) {

switch(position) {

case 0:

return new CustomizedListView();

case 1:

return new CustomizedListViewn();

default:

return null;

}

}

@Override

public CharSequence getPageTitle(int position) {

return ViewPagerExample.TITLETAB[position % ViewPagerExample.TITLETAB.length].toUpperCase();

}

@Override

public void destroyItem(View collection, int position, Object view) {

((ViewPager) collection).removeView((View) view);

}

}}

ListView Fragment:

public class CustomizedListView extends Fragment {

// All static variables

static final String URL = "http://url.net/booking/music.xml";

// XML node keys

static final String KEY_SONG = "song"; // parent node

static final String KEY_ID = "id";

static final String KEY_TITLE = "title";

static final String KEY_ARTIST = "artist";

static final String KEY_DURATION = "duration";

static final String KEY_THUMB_URL = "thumb_url";

ListView list;

LazyAdapter adapter;

XMLParser parser = new XMLParser();

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

new getFeed().execute();

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState)

{

View thisfragment1 = inflater.inflate(R.layout.main, container, false);

return thisfragment1;

}

private class getFeed extends AsyncTask<Void, Void, Document> {

@Override

protected Document doInBackground(Void... params) {

XMLParser parser = new XMLParser();

String xml = parser.getXmlFromUrl(URL); // getting XML from URL

Document doc = parser.getDomElement(xml); // getting DOM element

return doc;

}

@Override

protected void onPostExecute(Document doc) {

ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

NodeList nl = doc.getElementsByTagName(KEY_SONG);

// looping through all song nodes <song>

for (int i = 0; i < nl.getLength(); i++) {

// creating new HashMap

HashMap<String, String> map = new HashMap<String, String>();

Element e = (Element) nl.item(i);

// adding each child node to HashMap key => value

map.put(KEY_ID, parser.getValue(e, KEY_ID));

map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));

map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));

map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));

map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

// adding HashList to ArrayList

songsList.add(map);

}

list=(ListView)getActivity().findViewById(R.id.list);

// Getting adapter by passing xml data ArrayList

adapter=new LazyAdapter(getActivity(), songsList);

list.setAdapter(adapter);

// Click event for single list row

list.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

}

});

}

}}

The second fragment is exactly same as above, only the URL for RSS feed is different. Any help appreciated. Thanks

网友答案:

Please add the line list.notifyDataSetChanged(); After setting adapter in both the fragments CustomizedListView & CustomizedListViewn

分享给朋友:
您可能感兴趣的文章:
随机阅读: