Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

android - Checkbox gets unchecked when i scroll my custom listview

when i have more items in listview and i select some checkbox and scroll it down to select more checkbox then checkbox which were selected above gets deselect.

Plz help. where i can save checked state and position and when i do refresh list. code given below.

     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;
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i find some workaround solution not the best one but do his job what it do its on checkBox listener if view(CheckBox) not visible anymore its not alowed to change checkbox state so when you scroll its will not uncheck your check box still you need to provide List of position where checkbox was selected and on getView set state of checkbox

@Override
public 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 look like this

 @Override
public 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;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...