【问题标题】:checkbox checked in custom listview and maintain its state?在自定义列表视图中选中复选框并保持其状态?
【发布时间】:2013-06-14 06:15:36
【问题描述】:

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

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

            }
        });

下面是屏幕截图,我只检查了第一项,如下图所示。

然后我向上和向下滚动而不是得到如下图所示的结果。

谢谢。

【问题讨论】:

标签: android android-listview custom-lists


【解决方案1】:

在 getView() 中你应该先取消设置监听器

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

this 可能会帮助你。注意第 622 行。

【讨论】:

  • 我已经看到了,但仍然无法帮助我得到奇怪的结果。
  • @GirishBhutiya 也许你应该把 setTag() 放在 buttonView.getTag() 之前,但是我认为没有必要使用 setTag(position),值 getPosition alawys 等于 position
【解决方案2】:

您是否尝试过使用:

&lt;activity name= ".YourActivity" android:configChanges="screenSize"/&gt;

希望这会有所帮助。让我知道是否没有。

【讨论】:

  • 我有问题在 listView 滚动不是在活动中。
  • UI 绑定到活动。您是否尝试过使用上述参数。使用的参数
猜你喜欢
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多