Android: FragmentStatePageAdapter not updating ListView -
i have android application. application minsdk:15
i have navigationdrawer. when click on first navigation drawer menu item, new fragment appears called mainfragment. good, in android tutorial.
now in fragment need implement simple viewpager. viewpager page fragments.
here code:
public class mainfragent extends fragment @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.main_fragment_layout, container, false); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); arraylist<books> books; bookspageadapter contentpageadapter = new bookspageadapter(getfragmentmanager(), books); viewpager pager = (viewpager) getactivity().findviewbyid(r.id.viewpager); pager.setadapter(contentpageadapter); } private class bookspageadapter extends fragmentstatepageradapter { private arraylist<books> books; public bookspageadapter(fragmentmanager fm, private arraylist<books> books) { super(fm); this.books = books; } @override public fragment getitem(int pos) { return booksfragment.newinstance(books); } @override public int getcount() { return 5; } } }
and booksfragment:
public class booksfragment extends fragment { private arraylist<books> books; static contentfragment newinstance(final arraylist<books> books) { booksfragment booksfragment = new booksfragment(); bundle args = new bundle(); args.putserializable("books", books); contentfragment.setarguments(args); return booksfragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); books = (arraylist<books>) getarguments().getserializable(“books”); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_books, container, false); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } @override public void onresume() { super.onresume(); updateui(books); } public void updateui(arraylist<books> books) { listview listview = (listview)getactivity().findviewbyid(r.id.list_view); listview.setadapter(new myarrayadapter(getactivity(), books)); } private class myarrayadapter extends arrayadapter<books> { public myarrayadapter(final context context, final list<books> list) { super(context, r.layout.books_menu_item, r.id.title, list); } @override public view getview(final int position, final view convertview, final viewgroup parent) { final view row = super.getview(position, convertview, parent); book book = getitem(position); textview.settext(book.gettitle()); return row; } } }
the firstpage looks awesome, should. see list of books titles. on second page, list view empty. other views on second page (like textview..) fine except listview. wrong ?
p.s fragmentstatepageadapter support13. fragments android package, not support4
Comments
Post a Comment