我在列表视图中有一个复选框,我想知道是否已选中

时间:2018-11-04 17:18:29

标签: android

我有一个ID为“ lista_pessoas_checkbox”的列表视图,其中的项目基本上是一个文本字段,后跟一个复选框(名为“ check”),我想浏览一下这些项目以了解是否已选中该复选框。这是我当前的代码:

ListView listaPessoas = (ListView) findViewById(R.id.lista_pessoas_checkbox);

for (int i = 0; i < listaPessoas.getAdapter().getCount(); i++){
    View view = (View) listaPessoas.getAdapter().getItem(i);
    if (((CheckBox) view.findViewById(R.id.check)).isChecked()){
        novaParte.getPessoas().add(arrayPessoas.get(i));
    }
}

1 个答案:

答案 0 :(得分:3)

您可以将侦听器添加到列表视图项的每个属性。为此,您需要像下面那样实现您的适配器:

public class X extends ArrayAdapter<Model> {
            private final List<Model> list;
            private final Activity context;

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

       public Model getItem(int position) {
        return list.get(position);}

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

这里的模型是

public class Model {

    private String name;
    private boolean selected;

    public Model(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

}

现在您需要在活动或片段上调用此适配器

 ArrayAdapter<Model> adapter = new X(this,
                    getModel());
            setListAdapter(adapter);

要一次获取所有物品,只需致电:

 adapter.getList();

要通过位置致电获取商品:

adapter.getItem(position)