android - How to use ListFragment with BaseAdpater and Json file -
i have json displaying fine in listview when using customadapter extends baseadpater. in tests, can parse , inflate listview fine when reading json file when not loading fragment.
i need move this/display in fragment. fragment extending listfragment. cannot listfragment display.
this code fragment:
public class activitysearchresultstwo extends listfragment { private listfragment mlistview; public static activitysearchresultstwo newinstance() { activitysearchresultstwo fragment = new activitysearchresultstwo(); return fragment; } public activitysearchresultstwo() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view root = inflater.inflate(r.layout.fragment_session_row, null); return root; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); mlistview = (listview) findviewbyid(r.id.list); arraylist<sessions> sessionslist = sessions.getrecipesfromfile("recipes.json",getactivity()); sessionslistadapter adapter = new sessionslistadapter(getactivity(),sessionslist); setlistadapter(adapter); //getlistview().setonitemclicklistener(this); }
}
my listview declared follows:
<listview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list" android:background="@color/colorprimarylight"/>
this relevant extract object class
public class sessions { public static arraylist<sessions> getrecipesfromfile(string filename, context context){ final arraylist<sessions> recipelist = new arraylist<>();
// rest of parsing of json file....//
and adapter listing....
public class sessionslistadapter extends baseadapter { private context mcontext; private layoutinflater minflater; private arraylist<sessions> mdatasource; public sessionslistadapter(context context, arraylist<sessions> items) { mcontext = context; mdatasource = items; minflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); } //1 @override public int getcount() { return mdatasource.size(); } //2 @override public object getitem(int position) { return mdatasource.get(position); } //3 @override public long getitemid(int position) { return position; } //4 @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; // 1 if(convertview == null) { // 2 convertview = minflater.inflate(r.layout.fragment_session_row, parent, false); // 3 holder = new viewholder(); //holder.noofplacestextview = (textview) convertview.findviewbyid(r.id.noofplaces); holder.remainingplacestextview = (textview) convertview.findviewbyid(r.id.remainingplaces); holder.activitytimetextview = (textview) convertview.findviewbyid(r.id.activitytime); holder.activitypricetextview = (textview) convertview.findviewbyid(r.id.activityprice); // 4 convertview.settag(holder); } else{ // 5 holder = (viewholder) convertview.gettag(); } // 6 // textview noofplacestextview = holder.noofplacestextview; textview remainingplacestextview = holder.remainingplacestextview; textview activitytimetextview = holder.activitytimetextview; textview activitypricetextview = holder.activitypricetextview; sessions session = (sessions) getitem(position); // 2 // noofplacestextview.settext(session.places); remainingplacestextview.settext(session.remaining + " / " + session.places + " left"); activitytimetextview.settext(session.eventtime); activitypricetextview.settext(session.price); // 3 //picasso.with(mcontext).load(provider.imageurl).placeholder(r.mipmap.ic_launcher).into(thumbnailimageview); return convertview; } private static class viewholder { public textview remainingplacestextview; public textview activitytimetextview; public textview activitypricetextview; }
}
any appreciated list displaying in fragment. thanks
found problem, did not update oncreateview in fragment activity list layout...i still had set show previous layout using test.
view root = inflater.inflate(r.layout.fragment_session_list, container, false);
setting below solved issue.
@ishmaelmakitla, time on topic
Comments
Post a Comment