android - GetView return 0 for last item again? -
i trying display images , text grid view. getting text database , store arraylist later use. same getting images form sd card , display it.
the code write working nice problem getview() parameter position not work normally. position start 0 , end 0. last item of grid same first. bellow code
public class imageadapter extends baseadapter { private layoutinflater inflater; public imageadapter() { // todo auto-generated constructor stub inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub return listmainproduct.size(); // return arrgrpname.size(); } @override public object getitem(int position) { // todo auto-generated method stub return null; } @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 viewholder holder = null; if(convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.album_item, null); holder.imgview = (imageview)convertview.findviewbyid(r.id.thumbimage); holder.txtview = (textview)convertview.findviewbyid(r.id.tv); holder.txtview.settext(arrgrpname.get(position).tostring()); //log.v("text ", "text :- "+arrgrpname.get(position).tostring()); log.v("position ", "position :- "+position); bitmapfactory.options options=new bitmapfactory.options(); options.insamplesize = 2; string path = "/mnt/sdcard/js_images"; file imgfile = new file(path+"/"+arrimagename.get(position).tostring()+".jpg"); // log.v("path", ""+(path+"/"+arrimagename.get(position).tostring())+".jpg"); bitmap mybitmap = null; if(imgfile.exists()) { mybitmap = bitmapfactory.decodefile(imgfile.getabsolutepath(),options); // mybitmap = bitmapfactory.decodefile(arrimagename.get(position),options); holder.imgview.setimagebitmap(mybitmap); } else { } } else { holder = (viewholder) convertview.gettag(); } return convertview; } } class viewholder { imageview imgview; textview txtview; }
here log cat
08-16 12:40:45.614: v/position(27928): position :- 0 08-16 12:40:45.667: v/position(27928): position :- 1 08-16 12:40:45.687: v/position(27928): position :- 2 08-16 12:40:45.691: v/position(27928): position :- 3 08-16 12:40:45.710: v/position(27928): position :- 4 08-16 12:40:45.717: v/position(27928): position :- 5 08-16 12:40:45.723: v/position(27928): position :- 6 08-16 12:40:45.743: v/position(27928): position :- 7 08-16 12:40:45.746: v/position(27928): position :- 8 08-16 12:40:45.751: v/position(27928): position :- 0
album.class
public class album extends activity { public static datasource datasource; private imageadapter adapter; list<getmainproduct> listmainproduct; list<getstylemst> liststylemst; /** * array of strings (for temp storage of data form database) **/ arraylist<string> arrgrpname = new arraylist<string>(); arraylist<string> arrimagename = new arraylist<string>(); arraylist<string> arrgrpno = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.album); datasource = new datasource(album.this); listmainproduct = datasource.getmainproductdata(); /** * getting imagefolder & imagename database **/ for(int = 1 ; <= listmainproduct.size() ; i++) { string repimg = null; string strimgs = datasource.getimagenameforalbum(i); if(strimgs!=null) { repimg = strimgs.replaceall("%20", " "); } // log.v("imagename ", ""+ strimgs); string strfolder = datasource.getimagefolderfromdb(i); // log.v("foldername ", ""+ strfolder); log.v("path ", ""+ strfolder+"/"+repimg); string path = strfolder+"/"+repimg; arrimagename.add(path); // log.v("img size ", "size :- "+arrimagename); } /** * getting groupname & grpno form database **/ for(int = 0 ; < listmainproduct.size() ; i++) { string strgrpname = listmainproduct.get(i).getgrpname(); int intgrpno = listmainproduct.get(i).getgrpno(); arrgrpname.add(strgrpname); log.v("arrname ", "arraytext :- "+arrgrpname.tostring()); arrgrpno.add(string.valueof(intgrpno)); log.v("arrno ", "arraytextno :- "+arrgrpno.tostring()); } gridview gridview = (gridview)findviewbyid(r.id.albumgrid); adapter = new imageadapter(); gridview.setadapter(adapter); /** * when grid item selected or clicked **/ gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { // todo auto-generated method stub toast.maketext(album.this, ""+arrgrpno.get(position).tostring(), toast.length_short).show(); intent = new intent(album.this,gallery.class); i.putextra("grpno", arrgrpno.get(position).tostring()); startactivity(i); } }); }
please need serious don't know happen here in advance
in if(convertview==null)
block reference of views
, settag
them, in else part previous tag. apply other things after else
{ holder = (viewholder) convertview.gettag(); }
make following changes , try
viewholder holder = null; if(convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.album_item, parent,false); holder.imgview = (imageview)convertview.findviewbyid(r.id.thumbimage); holder.txtview = (textview)convertview.findviewbyid(r.id.tv); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.txtview.settext(arrgrpname.get(position).tostring()); //log.v("text ", "text :- "+arrgrpname.get(position).tostring()); log.v("position ", "position :- "+position); bitmapfactory.options options=new bitmapfactory.options(); options.insamplesize = 2; string path = "/mnt/sdcard/js_images"; file imgfile = new file(path+"/"+arrimagename.get(position).tostring()+".jpg"); //log.v("path", ""+(path+"/"+arrimagename.get(position).tostring())+".jpg"); bitmap mybitmap = null; if(imgfile.exists()) { mybitmap = bitmapfactory.decodefile(imgfile.getabsolutepath(),options); //mybitmap = bitmapfactory.decodefile(arrimagename.get(position),options); holder.imgview.setimagebitmap(mybitmap); } else { } return convertview;
Comments
Post a Comment