当我在ListvIEw中有更多项目时,我选择一些复选框并向下滚动以选择更多复选框,然后在上面选择的复选框取消选择.
Plz的帮助.在哪里我可以保存检查状态和位置,当我做@R_786_6419@.
代码如下.
protected voID onCreate(Bundle savedInstanceState) { dladapter = new Differencelistadapter(this, prodCodeArr, prodDescArr, serialBatchArr, expDtArr, qtyArr, serialNo, 0); diffeneceLv.setAdapter(dladapter);} static class VIEwHolder { TextVIEw srNotv, prodCodetv, prodDesctv, serialBatchNotv, expDatetv, qtytv; CheckBox consCheckBox;} public class Differencelistadapter extends BaseAdapter{ Context c; String[] prodCode, prodDesc, expDate, qty, serialNo, serialBatchNo; //TextVIEw srNotv, prodCodetv, prodDesctv, serialBatchNotv, expDatetv, qtytv; EditText consumedEt, disputeEt, OTCEt, patIEntnameEt; //final ArrayList<String> chkList = new ArrayList<String>(); ArrayList<Boolean> chkState; //CheckBox consCheckBox; private boolean checked = false; VIEwHolder vIEwHolder; VIEw row; int f=0;public Differencelistadapter(Context c, String[] prodCode, String[] prodDesc, String[] serialBatchNo, String[] expDate, String[] qty, String[] serialNo, int f){ this.c = c; this.prodCode= prodCode; this.prodDesc= prodDesc; this.serialBatchNo= serialBatchNo; this.expDate = expDate; this.qty=qty; this.serialNo= serialNo; this.f= f; chkState = new ArrayList<Boolean>(); }public String[] getChecked(){ return chkList.toArray(new String[chkList.size()]);} public int getCount() { // Todo auto-generated method stub return prodCode.length; } public Object getItem(int arg0) { // Todo auto-generated method stub return serialBatchNo[arg0]; } public long getItemID(int position) { // Todo auto-generated method stub return position; } public CheckBox getCheckBox() { return vIEwHolder.consCheckBox; } public voID toggleChecked() { checked = !checked; } public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { // Todo auto-generated method stub final int p= position; //if (convertVIEw == null) { vIEwHolder=new VIEwHolder(); LayoutInflater inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row= inflater.inflate(R.layout.consumedstockitemrow, null); vIEwHolder.consCheckBox = (CheckBox) row.findVIEwByID(R.ID.conschkbx); vIEwHolder.srNotv=(TextVIEw) row.findVIEwByID(R.ID.rowcdiffSrNoTv); vIEwHolder.prodCodetv=(TextVIEw) row.findVIEwByID(R.ID.rowcdiffProductCodeTv); vIEwHolder.prodDesctv=(TextVIEw) row.findVIEwByID(R.ID.rowcdiffProdDescTv); vIEwHolder.serialBatchNotv=(TextVIEw) row.findVIEwByID(R.ID.rowcdiffSerialBatchTv); vIEwHolder.expDatetv=(TextVIEw) row.findVIEwByID(R.ID.rowcdiffExpDtTv); vIEwHolder.qtytv=(TextVIEw) row.findVIEwByID(R.ID.rowcdiffQtyTv); vIEwHolder.consCheckBox.setonCheckedchangelistener(new OnCheckedchangelistener() { public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) { // Todo auto-generated method stub CheckBox cb = (CheckBox) buttonVIEw; if(cb.isChecked()== true) { if(chkList.contains(serialBatchNo[p])) { } else { chkList.add(serialBatchNo[p]); } } else { if(chkList.contains(serialBatchNo[p])) { chkList.remove(serialBatchNo[p]); } } } }); vIEwHolder.srNotv.setText(String.valueOf(p+1)); vIEwHolder.prodCodetv.setText(prodCode[p].toString()); vIEwHolder.prodDesctv.setText(prodDesc[p].toString()); vIEwHolder.serialBatchNotv.setText(serialBatchNo[p].toString()); vIEwHolder.expDatetv.setText(expDate[p].toString()); vIEwHolder.qtytv.setText(qty[p].toString()); return row; }}
解决方法:
我找到了一些解决方法
不是最好的,而是做他的工作
如果视图(CheckBox)不再可见,它在checkBox监听器上执行的 *** 作
它不能改变复选框状态
所以当你滚动它不会取消选中你的复选框
仍然需要提供选中复选框的位置列表
并在getVIEw设置状态的复选框
@OverrIDepublic voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) { if(buttonVIEw.isShown()) // chek if checkBox is visible { int position = buttonVIEw.getID(); // get position of check Box _selected[position] = buttonVIEw.isChecked(); // set true or false in List of seleted checkBoxes }}
getVIEw看起来像这样
@OverrIDepublic VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parentVIEw) { VIEw vIEw = convertVIEw; if (vIEw == null) { LayoutInflater inflater = activity.getLayoutInflater(); vIEw = inflater.inflate(R.layout.layout, null); VIEwHolder vIEwHolder = new VIEwHolder(); vIEwHolder.tv = (TextVIEw) rowVIEw.findVIEwByID(R.ID.text); vIEwHolder.cb = (CheckBox) rowVIEw.findVIEwByID(R.ID.check_Box); vIEwHolder.cb.setonCheckedchangelistener(this); vIEw.setTag(vIEwHolder); } VIEwHolder holder = (VIEwHolder) vIEw.getTag(); holder.tv.setText("Text"); holder.cb.setChecked(_selected[position]); //Geting state of check Box from array holder.cb.setID(position); // set check Box ID(position in adapter) return vIEw;}
VIEwHolder
class VIEwHolder{ TextVIEw tv; CheckBox cb;}
总结 以上是内存溢出为你收集整理的android – 当我滚动自定义列表视图时,复选框被取消选中全部内容,希望文章能够帮你解决android – 当我滚动自定义列表视图时,复选框被取消选中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)