简体   繁体   中英

checkbox checked in custom listview and maintain its state?

I have use below code for maintain checkbox state but its not working. I want only two checkbox is checked.

public class CACompareList extends ArrayAdapter<CompareListData>{

    public static int count = 0;
    public LayoutInflater inflater;
    public static ArrayList<CompareListData> selectedId;
    public ArrayList<CompareListData> listObjects;
    Context contex;
    public CACompareList(Context context, int textViewResourceId,
            ArrayList<CompareListData> objects) {
        super(context, textViewResourceId, objects);
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listObjects = objects;
        selectedId = new ArrayList<CompareListData>();
        this.contex = context;
    }
    public static class ViewHolder
    {   
        TextView txtViewLoanName;
        TextView txtViewHtmlString;
        CheckBox chkSelected;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_comparelist, null);

            holder.txtViewLoanName= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loanname);
            holder.txtViewHtmlString= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loandetail);
            holder.chkSelected= (CheckBox) convertView.findViewById(R.id.rowcomparelist_chk_selected);
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
        }

        final CompareListData data = this.listObjects.get(position);

        holder.txtViewLoanName.setText(this.listObjects.get(position).getLoanName());
        holder.txtViewHtmlString.setText(Html.fromHtml(this.listObjects.get(position).getHtmlString()));


        holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //listObjects.get(position).setSelected(isChecked);
                if(isChecked){
                    count++;

                }else{
                    count--;
                    selectedId.remove(data);
                }
                if(count >= 3)// it will allow 3 checkboxes only
                {
                    Toast.makeText(contex, "Select only two Loan", Toast.LENGTH_LONG).show();
                    buttonView.setChecked(false);
                    count--;
                }
                else
                {
                    selectedId.add(data);
                    buttonView.setSelected(isChecked);
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    listObjects.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                    Log.e("Selected Position is:", String.valueOf(getPosition));
                }
            }
        });
        holder.chkSelected.setTag(position);
        holder.chkSelected.setSelected(this.listObjects.get(position).getSelected());
        Log.e("Position is:", String.valueOf(position));
        return convertView;
    }

}

when i checked first checkbox and than scroll down and up I get different result my first checkbox is unchecked and another one is selected.

I have also checked on listView item click listner but it also doen't help me.

lvLoanLiat.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                if(IN_EDIT_MODE == false){
                    CompareListData data = (CompareListData)arg0.getItemAtPosition(arg2);

                    CheckBox cBox = (CheckBox) arg1.findViewById(R.id.rowcomparelist_chk_selected);
                    if(cBox.isChecked()){
                        count--;
                        selectedId.remove(data);
                    }else{
                        count++;
                    }
                    if(count >= 3)// it will allow 3 checkboxes only
                    {
                        Toast.makeText(CompareListActivity.this, "Select only two Loan", Toast.LENGTH_LONG).show();
                        cBox.setChecked(false);
                        count--;
                    }
                    else
                    {
                        selectedId.add(data);
                        cBox.setSelected(true);
                        data.setSelected(true);
                        Log.e("Selected Position is:", String.valueOf(arg2));
                    }
                }

            }
        });

Below is screen shots first i checked only first item as like below screenshots. 在此输入图像描述

Than I scroll Up and down than i get result like below image.

在此输入图像描述

Thanks.

In getView() you should unset the listener first

holder.chkSelected.setOnCheckedChangeListener(null);
holder.chkSelected.setSelected(isSelected);

and this maybe help you. Notice line 622.

Have you tried using:

<activity name= ".YourActivity" android:configChanges="screenSize"/>

Hope this helps . Lemme know if doesn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM