android - Custom Adapter getView() method is not called -
here code of fragment in setting custom adapter list.
there no errors listview
empty. have implemented getcount()
returns right number of items in arraylist. don't see ("inside", "getview")
in logcat
fragment
public class servicecarlistfragment extends fragment { private string url; private arraylist<cardetail> cardetaillist = new arraylist<cardetail>(); private carlistadapter adapter; private listview mlist; @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); url = getactivity().getintent().getstringextra("url"); new downloadcardetail().execute(url); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub view v = inflater.inflate(r.layout.fragment_service_car_list, container, false); mlist = (listview) v.findviewbyid(r.id.list); mlist.setadapter(adapter); (cardetail car : cardetaillist) { // start loading images each student car.loadimage(adapter); } return v; } class downloadcardetail extends asynctask<string, string, arraylist<cardetail>> { @override protected arraylist<cardetail> doinbackground(string... params) { // todo auto-generated method stub arraylist<cardetail> cardetaillist = jsonparser.parsejson(params[0]); return cardetaillist; } @override protected void onpostexecute(arraylist<cardetail> cardetaillist) { // todo auto-generated method stub servicecarlistfragment.this.cardetaillist = cardetaillist; log.d("dccs", string.valueof(servicecarlistfragment.this.cardetaillist.size())); adapter = new carlistadapter(getactivity(), servicecarlistfragment.this.cardetaillist); log.d("dccs", string.valueof((adapter.getcount()))); } } }
customadapter
public class carlistadapter extends baseadapter { private arraylist<cardetail> items = new arraylist<cardetail>(); private context context; public carlistadapter(context context, arraylist<cardetail> items) { this.context = context; this.items = items; } @override public int getcount() { // todo auto-generated method stub return items.size(); } @override public object getitem(int position) { // todo auto-generated method stub return items.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub log.d("inside", "getview"); layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service); viewholder holder = null; cardetail car = items.get(position); if (convertview == null) { convertview = minflater.inflate(r.layout.car_list_row, null); holder = new viewholder(); holder.tvcarname = (textview) convertview.findviewbyid(r.id.tvcarname); holder.tvdailypricevalue = (textview) convertview.findviewbyid(r.id.tvweeklypricevalue); holder.tvweeklypricevalue = (textview) convertview.findviewbyid(r.id.tvweeklypricevalue); holder.imgcar = (imageview) convertview.findviewbyid(r.id.imgcar); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.tvcarname.settext(car.getcarname()); if (car.getimage() != null) { holder.imgcar.setimagebitmap(car.getimage()); } else { // default image holder.imgcar.setimageresource(r.drawable.ic_action_call); } return convertview; } static class viewholder { textview tvcarname; textview tvdailypricevalue; textview tvweeklypricevalue; imageview imgcar; } }
the reasons getview
not called are:
getcount
returns 0- you forget call
setlistadapter
onlistview
. - if
listview
's visibility (or container's visibility)gone
. @taynãbonaldo valuable input listview
not attached viewport layout. is,mlistview = new listview(...)
used withoutmylayout.addview(mlistview)
in onpostexcute
, after create new instance of carlistadapter
suggest update new instance listview
. indeed need call again
mlist.setadapter(adapter);
edit: setadapter
should called on ui thread, avoid unexpected behaviours
edit2:
the same applies recyclerview
. make sure that
getitemcount
returning value grater0
(usually dataset size)- both
setlayoutmanager
,setadapter
have called onui thread
- the visibility of widget has set
visible
Comments
Post a Comment