android - Item in listview is repeating -
i have following arrayadapter .
public class smsarrayadapter extends arrayadapter<string> { list<string> smsbody; list<boolean> status; list<string> time; list<string> smsmessageid; context context; private static layoutinflater inflater = null; string fromnumber; public smsarrayadapter(context context, int resource, list<string> smsbody, list<boolean> status, list<string> time, list<string> smsmessageid, string fromnumber) { super(context, resource, smsbody); this.smsbody = smsbody; this.status = status; this.context = context; inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); this.fromnumber = fromnumber; this.time = time; this.smsmessageid=smsmessageid; } public string getstr(int position) { return smsbody.get(position); } public string getid(int position) { return smsmessageid.get(position); } public void setread(int position,string smsmessageid) { status.set(position, true); contentvalues values = new contentvalues(); values.put("read", true); context.getcontentresolver().update(uri.parse("content://sms/inbox"), values, "_id=" +smsmessageid, null); } @override public string getitem(int position) { // todo auto-generated method stub return smsbody.get(position); } public static class viewholder { public textview textfrom; public textview text_sms; public textview text_time; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { /****** inflate tabitem.xml file each row ( defined below ) *******/ convertview = inflater.inflate(r.layout.row_item, null); /****** view holder object contain tabitem.xml file elements ******/ holder = new viewholder(); holder.textfrom = (textview) convertview .findviewbyid(r.id.textview_from); holder.text_sms = (textview) convertview .findviewbyid(r.id.textview_sms); holder.text_time = (textview) convertview .findviewbyid(r.id.textview_time); holder.textfrom.settext(" " + fromnumber); string smstexttodisplay = smsbody.get(position); if (smstexttodisplay.length() > 100) smstexttodisplay = smstexttodisplay.substring(0, 99) + " ..."; holder.text_sms.settext(smstexttodisplay); holder.text_time.settext(time.get(position)); if (status.get(position) == false) { convertview.setbackgroundcolor(context.getresources().getcolor( r.color.light_blue_overlay)); } /************ set holder layoutinflater ************/ convertview.settag(holder); } else holder = (viewholder) convertview.gettag(); return convertview; } }
in customized list view items repeating.position of item same item. error ? how can avoid error ?
the code of set adapter follows :
arrayadapter = new smsarrayadapter(this, r.layout.row_item, smsbody, status, time, smsmessageid, fromnumber); smslistview.setadapter(arrayadapter); smslistview.setonitemclicklistener(this);
that because implementation of viewholder wrong. not setting data onto recycled views. can remove else block , set data.
viewholder holder; if (convertview == null) { /****** inflate tabitem.xml file each row ( defined below ) *******/ convertview = inflater.inflate(r.layout.row_item, null); /****** view holder object contain tabitem.xml file elements ******/ holder = new viewholder(); holder.textfrom = (textview) convertview .findviewbyid(r.id.textview_from); holder.text_sms = (textview) convertview .findviewbyid(r.id.textview_sms); holder.text_time = (textview) convertview .findviewbyid(r.id.textview_time); } /************ set holder layoutinflater ************/ convertview.settag(holder); } holder = (viewholder) convertview.gettag(); holder.textfrom.settext(" " + fromnumber); string smstexttodisplay = smsbody.get(position); if (smstexttodisplay.length() > 100) smstexttodisplay = smstexttodisplay.substring(0, 99) + " ..."; holder.text_sms.settext(smstexttodisplay); holder.text_time.settext(time.get(position)); if (status.get(position) == false) { convertview.setbackgroundcolor(context.getresources().getcolor( r.color.light_blue_overlay)); return convertview; }
in addition i'd recommend using recyclerview instead of listview.
Comments
Post a Comment