android - How to display custom ListFragment on MainActivity's tab using custom ArrayAdapter -
android - How to display custom ListFragment on MainActivity's tab using custom ArrayAdapter -
ok have main activity 3 tabs made standard android template (actionbar tabs viewpager). have list<appinfo>
has info want display on 2nd tab in listview 2 text fields per row.
the code below organized such:
my arrayadapter
has list<appinfo>
. uses app_info_row_layout.xml
my listfragment
sets listadapter arrayadapter
. uses list_fragment.xml
my fragmentpageradapter
creates listfragment
in getitem()
method
here arrayadapter class
public class appinfolist extends arrayadapter<appinfo>{ private list<appinfo> list; private final context context; public appinfolist(context context, int resource) { super(context, resource); this.context = context; list = // list objects generated } @override public view getview(int position, view convertview, viewgroup parent){ appinfo appinfo = list.get(position); layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); if(convertview == null){ convertview = (view) inflater.inflate(r.layout.app_info_row_layout, null, false); } // text field 1 textview appname = (textview) convertview.findviewbyid(r.id.appname); appname.settext(appinfo.packagename); // text field 2 textview totaltime = (textview) convertview.findviewbyid(r.id.totaltime); totaltime.settext( " minutes" ); // took out logic create code shorter homecoming convertview; } }
the above uses simple app_info_row_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/appname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/appname" android:textsize="30sp" > </textview> <textview android:id="@+id/totaltime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/totaltime" android:textsize="20sp" > </textview> </linearlayout>
and here custom listfragment that's supposed utilize above arrayadapter , display info in listview
public class appinfolistfragment extends listfragment{ @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view rootview = inflater.inflate(r.layout.list_fragment, container, false); homecoming rootview; } @override public void onactivitycreated(bundle savedinstancestate){ super.onactivitycreated(savedinstancestate); appinfolist appinfoadapter = new appinfolist(getactivity(), 0); setlistadapter(appinfoadapter); } }
using next list_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> <textview android:id="@android:id/empty" android:layout_width="match_parent" android:layout_height="wrap_content" > </textview> </linearlayout>
i've extended fragmentpageradapter , created following
public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // homecoming placeholderfragment 1st , 3rd tab // homecoming different views/fragments here if(position == 1){ if(appinfolistfragment == null) appinfolistfragment = new appinfolistfragment(); homecoming appinfolistfragment; } else homecoming placeholderfragment.newinstance(position + 1); } @override public int getcount() { // show 3 total pages. homecoming 3; } }
when run application, 2nd tab blank instead of showing list. doing wrong?
i utilize next setup:
array adapter class:
public class appinfolist extends arrayadapter<appinfo>{ private list<appinfo> list; private final context context; public appinfolist(context context, list<appinfo>) { super(context, r.layout.rowlayout, list); this.context = context; list = list; } @override public view getview(int position, view convertview, viewgroup parent){ appinfo appinfo = list.get(position); layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); if(convertview == null){ convertview = (view) inflater.inflate(r.layout.app_info_row_layout, null, false); } // text field 1 textview appname = (textview) convertview.findviewbyid(r.id.appname); appname.settext(appinfo.packagename); // text field 2 textview totaltime = (textview) convertview.findviewbyid(r.id.totaltime); totaltime.settext( " minutes" ); // took out logic create code shorter homecoming convertview; } }
all other classes seem fine. reference, used next tutorial help me.
android android-listview android-arrayadapter android-listfragment
Comments
Post a Comment