复选框在自定义列表视图中检查并保持其状态?

时间:2013-06-18 06:31:25

标签: android android-listview custom-lists

我使用下面的代码来维护复选框状态,但它不起作用。我只想检查两个复选框。

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

}

当我选中第一个复选框而不是向下滚动时,我得到不同的结果我的第一个复选框未选中,另一个被选中。

我还检查了listView项目点击列表器,但它也没有帮助我。

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

            }
        });

下面是屏幕截图我首先检查了第一项,如下面的屏幕截图所示。 enter image description here

我向上和向下滚动比得到如下图像的结果。

enter image description here

感谢。

2 个答案:

答案 0 :(得分:1)

在getView()中,您应首先取消设置侦听器

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

this可能对您有所帮助。注意第622行。

答案 1 :(得分:0)

您是否尝试过使用:

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

希望这会有所帮助。 Lemme知道是不是。