以编程方式切换复选框

时间:2011-11-09 06:45:19

标签: android listview android-arrayadapter

我有一个需要检查/取消选中项目的ListView。我已经设置了一个当前使用android.R.layout.simple_list_item_multiple_choice作为行的ArrayAdapter,一切都显示正常。我也能够正确获得此项目的点击次数。但是,选择项目时,UI 中的复选框不会切换。我一直试图弄清楚这一点,有人能指出我正确的方向吗?我只是想知道如何强制UI更新以反映复选框的更改状态。

如果需要,我可以提供代码,但我正在尝试寻找一些非常具体的内容,所以我想发布一堆我的代码对我没什么帮助。

谢谢!

5 个答案:

答案 0 :(得分:26)

以编程方式在java代码中

CheckBox mCheckBox = (CheckBox) findViewById(R.id.checkBox);

mCheckBox.setChecked(true); //to check
mCheckBox.setChecked(false); //to uncheck

在android XML中

android:checked="true" //to check
android:checked="false" //to uncheck

作为

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Checkbox Item" />

答案 1 :(得分:22)

首先回答我的回答:Android listview with check boxes?

很好,因为你想在ListView中实现选中/未选中的复选框,你只需要在getView()方法中实现以下行:

 // also check this lines in the above example
ViewHolder holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(list.get(position).isSelected());

还检查getView()方法,以便在ListView中驻留的CheckBox上实现事件:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox
            .setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Model element = (Model) viewHolder.checkbox
                            .getTag();
                    element.setSelected(buttonView.isChecked());
                }
            });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    // ......  
}

答案 2 :(得分:2)

您是否尝试过以下操作?

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {

    //Invert the checkbox-status        
    ((CheckedTextView) view.findViewById(R.id.text1)).setChecked(!isChecked());

    return;

}   

答案 3 :(得分:0)

您可以使用自定义基准适配器。这将允许您一次只选择一个,但您可以通过撤消检查以查看选择了哪一个来更改此项。

public class CheckBoxGridAdapter extends BaseAdapter{

    LayoutInflater inflater;
    ArrayList<Holder> list;
    int res;
    Context context;

    public CheckBoxGridAdapter(Context context, ArrayList<Holder> list, int layoutResourceId) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.list =list;
        this.res = layoutResourceId;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) { 
        final ViewHolder holder; 
        if (convertView == null) { 
            convertView = inflater.inflate(res, 
                    parent, false);
            holder = new ViewHolder(); 
            holder.tv1 = (TextView) convertView.findViewById(R.id.checkBoxTxt); 
            holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox); 
           convertView.setTag(holder); 
       } else { 
           holder = (ViewHolder) convertView.getTag(); 
       } 
       Holder h = list.get(position);
       holder.tv1.setText(h.getCBName());
       holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,
                   boolean isChecked) {
               if(isChecked)
               {
                    int pos = (Integer) buttonView.getTag();
                    checkChecks(pos);               
               }
               CheckBoxGridAdapter.this.notifyDataSetChanged();
           }
       });
       holder.cb.setTag(position);
       holder.cb.setChecked(list.get(position).getSelectState());
       return convertView; 
    }

    protected void checkChecks(int pos) {
            for(int i = 0;i<list.size();i++)
            {
                Holder h = (Holder) list.get(i);
                if(i==pos)
                {
                  Log.d( pos +" checked","is checked");
                  h.setSelectState(true);
                }
                else
                {
                     Log.d( i +" checked","is  not checked");
                     h.setSelectState(false);
                }
            }
    }

    static class ViewHolder
    {
        TextView tv1;
        CheckBox cb;
        RadioButton rb;
    }

}

答案 4 :(得分:0)

您可以通过执行以下操作来切换复选框:

checkbox.setChecked(!checkbox.isChecked());

复选框是您的“复选框”元素。