android - custom SimpleCursorAdapter and BindView (buttons are randomly Invisible) -
i made custom simplecursoradapter can click on button inside listview (the button needs call someone) when there no phone number want button invisible, @ first code show correctly when scroll , down few times buttons randomly become invisible.
edited: when of them visible it's fine when 1 (button inside item on listview) become invisible starts randomly become invisible.
my code:
public mycursoradapter(context context, int layout, cursor c, string[] from, int[] to, double lat, double lon) { super(context, layout, c, from, to); latc = lat; lonc = lon; } public mycursoradapter(context context, int layout, cursor c, string[] from, int[] to) { super(context, layout, c, from, to); } @override public view newview(context context, cursor cursor, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view view = inflater.inflate(r.layout.list_view_place_list, parent, false); return view; } @override public void bindview(final view view,final context context,final cursor cursor) { sharedpreferences settings = preferencemanager.getdefaultsharedpreferences(context); string name = cursor.getstring(cursor.getcolumnindex(placesdb.place_name)); string address = cursor.getstring(cursor.getcolumnindex(placesdb.place_address)); double lat = cursor.getdouble(cursor.getcolumnindex(placesdb.place_lat)); double lon = cursor.getdouble(cursor.getcolumnindex(placesdb.place_lon)); final string phone = cursor.getstring(cursor.getcolumnindex(placesdb.place_phone)); textview txtname = (textview) view.findviewbyid(r.id.txtname); textview txtaddress = (textview) view.findviewbyid(r.id.txtaddress); textview txtdistance = (textview) view.findviewbyid(r.id.txtdistance); txtname.settext(name); txtaddress.settext(address); button btncall = (button) view.findviewbyid(r.id.btncall); if (latc != -1) { double dis = getdistancefromlatloninkm(latc, lonc, lat, lon); if (settings.getstring("mk", "km").equals("miles")) { dis = dis / 1.621371; txtdistance.settext(string.format("%.3f", dis) + " miles"); } else txtdistance.settext(string.format("%.3f", dis) + " km"); } else { txtdistance.settext("distance not available"); } if (phone == null){ btncall.setvisibility(view.invisible); } else { view.findviewbyid(r.id.btncall).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string tel; intent dialintent = new intent(); tel = phone.replace("-", ""); dialintent.setaction(intent.action_call); dialintent.setdata(uri.parse("tel:" + tel)); context.startactivity(dialintent); } }); } }
since android tries recycle unused listview-items have set gui elements
if (phone == null){ btncall.setvisibility(view.invisible); }
only made btncall invisible there no code make recycled invisible listview-item-btncall visible again.
if (phone == null){ btncall.setvisibility(view.invisible); } else { btncall.setvisibility(view.visible); }
should job.
for same reason code view.findviewbyid(r.id.btncall).setonclicklistener(...)
should execeted in newview()
added once.
Comments
Post a Comment