android - Using list view in fragments -
i have drawer layout fragment (in main activity) shows expandable list, when click on sub-item of expanded item, popup window appears. pop window custom layout contains text views, list view , 2 buttons.
i gave list view id: android:id="@android:id/list"
, return null viewgroup in oncreateview function follow:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_settings, null);
and make class extends fragment not listfragment
i did previous 3 steps avoid "content has view id attribute 'android.r.id.list' not listview class in listfragment" error
but still have nullpointerexception error pointing
faillistview.setadapter(adapter);
when click on "failsafe setup" item
i've seen suggestions of them don't useful, rootview .findviewbyid?() not getactivity().findviewbyid() ==> i've it. or declaring list view before oncreateview function. annoyed this, i've tried lot of suggestions work others.
even though, i've tried add snippet onactivitycreated function
testfailsafeadapter adapter = new testfailsafeadapter(getactivity().getapplicationcontext(), r.layout.dialog,r.id.textview1, getresources().getstringarray(r.array.failsafe_list)); faillistview.setadapter(adapter);
any appreciated.
the following custom layout used in popup window (dialog.xml):
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <linearlayout android:id="@+id/headers_container" android:layout_width="match_parent" android:layout_height="30dp" android:layout_margintop="5dp" android:orientation="horizontal" > <textview android:id="@+id/textview1" android:layout_width="100dp" android:layout_height="wrap_content" android:text="failsafe" android:textsize="16sp" /> <textview android:id="@+id/textview2" android:layout_width="35dp" android:layout_height="wrap_content" android:layout_torightof="@+id/textview1" android:text="on" android:textsize="16sp" /> </linearlayout> <listview android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="350dp" android:layout_alignparentleft="true" android:layout_below="@+id/headers_container" android:background="#ffeeaa" > </listview> <linearlayout android:id="@+id/linearlayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_below="@android:id/list" android:orientation="horizontal" > <button android:id="@+id/dialogbuttonreset" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginright="5dp" android:layout_margintop="5dp" android:text="reset" /> <button android:id="@+id/dialogbuttonsend" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginright="5dp" android:layout_margintop="5dp" android:text="send" /> </linearlayout> </relativelayout>
the following fragment has expandable list of drawable layout (settingsfragment.java):
public class settingsfragment extends fragment { expandablelistadapter listadapter; expandablelistview explistview; list<string> listdataheader; hashmap<string, list<string>> listdatachild; int position = 0; listview faillistview = null; public settingsfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // view rootview = inflater.inflate(r.layout.fragment_settings, container, false); view rootview = inflater.inflate(r.layout.fragment_settings, null); faillistview = (listview) rootview.findviewbyid(android.r.id.list); testfailsafeadapter adapter = new testfailsafeadapter(getactivity().getapplicationcontext(), r.layout.dialog,r.id.textview1, getresources().getstringarray(r.array.failsafe_list)); faillistview.setadapter(adapter); return rootview; } @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); // listview explistview = (expandablelistview) getactivity().findviewbyid(r.id.lvexp); // preparing list data preparelistdata(); listadapter = new expandablelistadapter(getactivity(), listdataheader, listdatachild); // setting list adapter explistview.setadapter(listadapter); // listview group click listener explistview.setongroupclicklistener(new ongroupclicklistener() { public boolean ongroupclick(expandablelistview parent, view v, int groupposition, long id) { return false; } }); // listview group expanded listener explistview.setongroupexpandlistener(new ongroupexpandlistener() { @override public void ongroupexpand(int groupposition) { toast.maketext(getactivity(), listdataheader.get(groupposition) + " expanded", toast.length_short).show(); } }); // listview group collasped listener explistview.setongroupcollapselistener(new ongroupcollapselistener() { @override public void ongroupcollapse(int groupposition) { toast.maketext(getactivity(), listdataheader.get(groupposition) + " collapsed", toast.length_short).show(); } }); // listview on child click listener explistview.setonchildclicklistener(new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, final int groupposition, final int childposition, long id) { // todo auto-generated method stub if(listdatachild.get(listdataheader.get(groupposition)).get(childposition).equals("usb") ){ fragmentmanager manager = getfragmentmanager(); /** instantiating dialogfragment class */ alertdialogradio alert = new alertdialogradio(); /** creating bundle object store selected item's index */ bundle b = new bundle(); /** storing selected item's index in bundle object */ b.putint("position", position); /** setting bundle object dialog fragment object */ alert.setarguments(b); /** creating dialog fragment object, in turn open alert dialog window */ alert.show(manager, "alert_dialog_radio"); } if(listdatachild.get(listdataheader.get(groupposition)).get(childposition).equals("failsafe setup") ){ toast.maketext(getactivity()," failsafe setup",toast.length_short).show(); // create dialog component final dialog dialog = new dialog(getactivity()); //tell dialog use dialog.xml it's layout description dialog.setcontentview(r.layout.dialog); dialog.settitle("failsafe setup"); listview faillistview = (listview) dialog.findviewbyid(android.r.id.list); button dialogbuttonreset = (button) dialog.findviewbyid(r.id.dialogbuttonreset); button dialogbuttonsend = (button) dialog.findviewbyid(r.id.dialogbuttonsend); dialogbuttonreset.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(getactivity()," failsafe reset",toast.length_short).show(); } }); dialogbuttonsend.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(getactivity()," failsafe send",toast.length_short).show(); } }); dialog.show(); } return false; } // end of onclickchild method }); } private void preparelistdata() { listdataheader = new arraylist<string>(); listdatachild = new hashmap<string, list<string>>(); // adding child data listdataheader.add("connection"); listdataheader.add("mission limits"); listdataheader.add("compass calibration"); listdataheader.add("sensor calibration"); listdataheader.add("failsafe"); // adding child data list<string> connection = new arraylist<string>(); connection.add("usb"); connection.add("wi-fi"); list<string> missionlimits = new arraylist<string>(); list<string> compasscalibration = new arraylist<string>(); list<string> sensorcalibration = new arraylist<string>(); list<string> failsafe = new arraylist<string>(); failsafe.add("failsafe setup"); listdatachild.put(listdataheader.get(0), connection); // header, child data listdatachild.put(listdataheader.get(1), missionlimits); listdatachild.put(listdataheader.get(2), compasscalibration); listdatachild.put(listdataheader.get(3), sensorcalibration); listdatachild.put(listdataheader.get(4), failsafe); }
}
npe here:
faillistview.setadapter(adapter);
because listview
android:id/list
id inside dialog.xml
layout dialog
layout instead of in fragment_settings.xml
settingsfragment
fragment layout.
so, using context or object of dialog it's not possible access listview in oncreateview
Comments
Post a Comment