0

我是 android 新手,任何人都可以帮助在复选框中获取密钥和相应的值(检查/不检查)。

复选框中可以有多个选中项。我必须获取用户在复选框中“选中”的所有项目。我在“onCheckedChanged”中使用以下代码。

viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        Model element = (Model) viewHolder.checkbox.getTag();
        //String temp = viewHolder.checkbox.getText().toString(); //didn't give me the key
        //Log.d(TAG, temp);
        //String temp = buttonView.getText().toString(); //didn't give me the key
        element.setSelected(buttonView.isChecked());
    }
});
4

3 回答 3

0

您在 buttonView 参数中获得了更改状态的控件。为什么不使用 findViewByiD() 在 onCreate 中保存指向控件的指针,然后与 onCheckedChange 中的所述指针进行比较?

于 2012-08-18T20:09:23.750 回答
0

对不起,我没有得到你的解决方案。我正在使用 arrayAdapter 来显示复选框。以下是处理“setOnCheckedChangeListener”的arrayAdapter 的代码

公共类 InteractiveArrayAdapter 扩展 ArrayAdapter {

private final List<Model> list;
private final Activity context;
private static final String TAG = "InteractiveArrayAdapter";

public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected CheckBox 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();
                        //String temp = viewHolder.checkbox.getText().toString();
                        //Log.d(TAG, temp);
                        //String temp = buttonView.getText().toString();
                        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());
    return view;
}

}

于 2012-08-19T04:05:57.163 回答
0

如果我理解您的问题,您是否正在寻找一种跟踪列表中所有复选框的方法?如果是这样,请尝试以下操作

private ArrayList<boolean> checkedList = new ArrayList<boolean>();

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
    LayoutInflater inflator = context.getLayoutInflater();
    convertView = 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);
    convertView.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       if (isChecked && !checkedList.contains(list.getPosition()))
          checkedList.add(list.getPosition());
       else if (!isChecked && checkedList.contains(list.getPosition()))   
          //TODO search and remove list item here
    }
});

return convertView;

}

于 2012-08-27T23:33:02.603 回答