android - ListView in Fragment not showing up -
i'm using library in fragment, when run app swipemenulistview not showing contents, here on stack found guys using listview inside oncreateview method, tried didn't work
public class autorizadasfragment extends fragment { private swipemenulistview listview; private customarrayadapter adapter; private list<string> data; public autorizadasfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_autorizadas, container, false); return rootview; } @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); data = new arraylist<string>(); data.add("teste 1"); data.add("teste 2"); data.add("teste 3"); data.add("teste 4"); data.add("teste 5"); listview = (swipemenulistview) getactivity().findviewbyid(r.id.list); adapter = new customarrayadapter(getactivity(), data); listview.setadapter(adapter); swipemenucreator creator = new swipemenucreator() { @override public void create(swipemenu menu) { // create "open" item swipemenuitem openitem = new swipemenuitem( getactivity()); // set item background openitem.setbackground(new colordrawable(color.rgb(0xc9, 0xc9, 0xce))); // set item width openitem.setwidth(90); // set item title openitem.settitle("open"); // set item title fontsize openitem.settitlesize(18); // set item title font color openitem.settitlecolor(color.black); // add menu menu.addmenuitem(openitem); // create "delete" item swipemenuitem deleteitem = new swipemenuitem( getactivity()); // set item background deleteitem.setbackground(new colordrawable(color.rgb(0xf9, 0x3f, 0x25))); // set item width deleteitem.setwidth(90); // set icon deleteitem.seticon(r.drawable.ic_exit); // add menu menu.addmenuitem(deleteitem); } }; listview.setmenucreator(creator); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { // todo auto-generated method stub super.oncreateoptionsmenu(menu, inflater); inflater.inflate(r.menu.menu_main, menu); } }
xml:
<linearlayout style="@style/formregister" android:orientation="vertical" android:weightsum="1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.baoyz.swipemenulistview.swipemenulistview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="match_parent" android:scrollbars="vertical"/> </linearlayout>
you can hold reference root view adding private attribute e.g.
private view rootview = null;
and change following line in oncreateview:
view rootview = inflater.inflate(r.layout.fragment_autorizadas, container, false);
to
rootview = inflater.inflate(r.layout.fragment_autorizadas, container, false);
then not need use "getactivity().findviewbyid(r.id.list);" (it become "rootview.findviewbyid(r.id.list);")
i proposing because not sure view created in oncreateview added activities root view when onactivitycreated() called.
Comments
Post a Comment