android - Listview items loosing changes on scrolling -
my need :
i want implement list of variables can have dynamic no of rows. list contains 3 textviews,1 imageview, 3imagebuttons.
user can select row tap. on long tap multiple items can selected. on selection selected row ui changed. selected item change background , image of imageview , 1 image button visible on selection.
as user tap on imagebutton image change of image button , other image buttons become visible
if items selected , user try select single row tapping other selected items removed. listview items should come original condition. imageview reset default image , imagebutton ll disappear.
what tried :
i try implement listview using base adapter. define custom row need of 3 textviews, 1 imageview , 3 imagebutton.
package com.astron.myapplication; import java.sql.date; import java.text.dateformat; import java.text.simpledateformat; import java.util.arraylist; import com.astron.myapplication.r; import com.astron.myapplication.tssvariableinfo; import android.content.context; import android.content.res.colorstatelist; import android.graphics.color; import android.util.typedvalue; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imagebutton; import android.widget.imageview; import android.widget.linearlayout; import android.widget.tablelayout; import android.widget.textview; import android.widget.listview; import org.w3c.dom.text; import android.widget.tablerow; import android.view.view; import android.graphics.drawable.bitmapdrawable; import android.graphics.bitmap; public class mycustombaseadapter extends baseadapter { tssvariableinfo info[]; private layoutinflater minflater; public mycustombaseadapter(context context, tssvariableinfo[] results) { info = new tssvariableinfo[results.length]; info = results; minflater = layoutinflater.from(context); } public int getcount() { return info.length; } public object getitem(int position) { return info[position]; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { final viewholder holder; dateformat dateformatfrom ; dateformat dateformatto; boolean isrolseleted = false; if (convertview == null) { convertview = minflater.inflate(r.layout.row, null); holder = new viewholder(); holder.variablecustomergivenname = (textview) convertview.findviewbyid(r.id.customergivenname); holder.variablestandardname = (textview) convertview.findviewbyid(r.id.standdardname); holder.variablesectionname = (textview) convertview.findviewbyid(r.id.sectionname); holder.variablevalue = (textview) convertview.findviewbyid(r.id.lastvalue); holder.variablelastdate = (textview) convertview.findviewbyid(r.id.lastdate); holder.variablelegendimage = (imageview)convertview.findviewbyid(r.id.imglegend); holder.variablelegendimage.settag(0); holder.btnmore = (imagebutton)convertview.findviewbyid(r.id.btnmore) ; holder.btnmore.settag(0); holder.btny2 = (imagebutton)convertview.findviewbyid(r.id.btn2y) ; holder.btntl = (imagebutton)convertview.findviewbyid(r.id.btntl) ; convertview.settag(holder); convertview.setbackgroundcolor(color.parsecolor("#e8e8e8")); } else { holder = (viewholder) convertview.gettag(); } holder.variablecustomergivenname.settext(info[position].variablecustomergivenname); holder.variablestandardname.settext(info[position].variablestandarname); holder.variablesectionname.settext(info[position].sectionname); holder.variablevalue.settext(info[position].value.tostring()); dateformatfrom = new simpledateformat("mm/dd/yyyy hh:mm:ss aa"); dateformatto = new simpledateformat("mm/dd/yy"); java.util.date dt; string strdt = info[position].lastdate; dt = dateformatfrom.parse(strdt); dateformatto.format(dt); holder.variablelastdate.settext(dateformatto.format(dt)); //holder.variablelastdate.settext(dateformatto.format(dateformatfrom.format(info[position].lastdate))); if(info[position].variablecustomergivenname.isempty() || info[position].variablecustomergivenname==null) { holder.variablecustomergivenname.settext(info[position].variablestandarname); holder.variablestandardname.settext(info[position].sectionname); holder.variablesectionname.setvisibility(view.invisible); } else holder.variablesectionname.setvisibility(view.visible); holder.btnmore.setvisibility(view.visible); return convertview; } public void updateadapter(tssvariableinfo[] results) { this.info= results; //and call notifydatasetchanged notifydatasetchanged(); } static class viewholder { textview variablecustomergivenname; textview variablestandardname; textview variablesectionname; textview variablevalue; textview variablelastdate; imageview variablelegendimage; imagebutton btnmore; imagebutton btny2; imagebutton btntl; }
my onclick
list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { ((imagebutton)((linearlayout) arg1).getchildat(0).findviewbyid(r.id.btnmore)).settag("1"); ((imagebutton)((linearlayout) arg1).getchildat(0).findviewbyid(r.id.btnmore)).setvisibility(view.visible); imageview imglegend = ((imageview) ((linearlayout) arg1).getchildat(0).findviewbyid(r.id.imglegend)); setimageforlegend(imglegend, info[position].variableid); });
facing problem :
after selection of single row on scrolling of listview other items showing change made single row.
first of all, dont use array[]
use bean
class , getter setter
, arraylist
bind listview
items take reference of below link how did custom listview using sqlite database
http://androidtuts4u.blogspot.in/2013/02/android-list-view-using-custom-adapter.html
to maintain changes need set changes in bean
class may need change code way you.
may answer confuse because dont know way. please follow above link read , implementation.
Comments
Post a Comment