android - Adapter returning same item twice -
i have listview holding items. shows result of search in database, 2 items meets criteria of select sentence.
when search first time, returns twice same item. but, when click search button again, time done ok, showing both items. items first time?
adapter:
public class listaexpedientesadapter extends baseadapter { private arraylist<beanlistaexpedientes> listaexpedientes; private layoutinflater inflater=null; private context c; public listaexpedientesadapter(context c, arraylist<beanlistaexpedientes> lista){ this.listaexpedientes=lista; inflater=layoutinflater.from(c); this.c=c; } @override public int getcount() { return listaexpedientes.size(); } @override public object getitem(int position) { return listaexpedientes.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if(convertview==null){ convertview=inflater.inflate(r.layout.search_exp_adapter_layout, null); holder=new viewholder(); holder.numexp=(textview)convertview.findviewbyid(r.id.textnumexp); holder.nombrecomercial=(textview)convertview.findviewbyid(r.id.textnombrecomercial); holder.nombrecompleto=(textview)convertview.findviewbyid(r.id.textnombrecompleto); holder.foto=(imageview)convertview.findviewbyid(r.id.searchfoto); holder.codficha=(textview)convertview.findviewbyid(r.id.textviewcodficha); holder.numexp.settext(string.valueof(listaexpedientes.get(position).getnumexp())); holder.nombrecompleto.settext(string.valueof(listaexpedientes.get(position).getnombrecompleto())); holder.nombrecomercial.settext(string.valueof(listaexpedientes.get(position).getnombrecomercial())); holder.foto.setimagebitmap(bitmap.createscaledbitmap(bitmapfactory.decodefile(listaexpedientes.get(position).getfoto()),dptopx(80), dptopx(80), true)); holder.codficha.settext(string.valueof(listaexpedientes.get(position).getcodficha())); }else{ holder = (viewholder) convertview.gettag(); } if(holder==null){ holder=new viewholder(); } return convertview; } private int dptopx(int dp) { float density = c.getresources().getdisplaymetrics().density; return math.round((float)dp * density); } static class viewholder{ textview numexp; textview nombrecompleto; textview nombrecomercial; imageview foto; textview codficha; } }
the piece of code sql sentence launched (its inside thread):
cursor csr=db.rawquery(sentencia, null); listaexpedientes=new arraylist<beanlistaexpedientes>(); if(csr.movetofirst()){ { codficha=csr.getlong(3); codexpediente=csr.getlong(15); nombrecompleto=csr.getstring(8); nombrecomercial=csr.getstring(13); cursor csr2=db.rawquery("select path fotos codficha="+codficha+" limit 1", null); if(csr2.movetofirst()){ path=csr2.getstring(0); } listaexpedientes.add(new beanlistaexpedientes(path, nombrecomercial, nombrecompleto, codexpediente, codficha)); }while (csr.movetonext()); refreshadapter(); adapter.notifydatasetchanged();
those lines
holder.numexp.settext(string.valueof(listaexpedientes.get(position).getnumexp())); holder.nombrecompleto.settext(string.valueof(listaexpedientes.get(position).getnombrecompleto())); holder.nombrecomercial.settext(string.valueof(listaexpedientes.get(position).getnombrecomercial())); holder.foto.setimagebitmap(bitmap.createscaledbitmap(bitmapfactory.decodefile(listaexpedientes.get(position).getfoto()),dptopx(80), dptopx(80), true)); holder.codficha.settext(string.valueof(listaexpedientes.get(position).getcodficha()));
go outside if/else logic. getview
called @ least getcount
times, once convertview = null
edit,
after initalize holder
, when convertview
null, don't forget call convertview.settag(holder)
Comments
Post a Comment