如何将选中的项目添加到我的数组列表中?

时间:2019-01-21 05:01:38

标签: android arraylist recycler-adapter android-checkbox

嗨,我正在使用带有复选框的recyclerview,现在我要尝试的是用户检查的列表项,我想将这些项添加到我的arraylist中,现在它不起作用,并且它添加了所有列表项,而不是添加选中的项。以下是我的适配器代码。有谁能帮助我解决这个问题?

public static class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

        private LayoutInflater inflater;
        public static ArrayList<Model> imageModelArrayList;
        private Context ctx;


        public CustomAdapter(Context ctx, ArrayList<Model> imageModelArrayList) {

            inflater = LayoutInflater.from(ctx);
            this.imageModelArrayList = imageModelArrayList;
            this.ctx = ctx;
        }

        @Override
        public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = inflater.inflate(R.layout.rv_item, parent, false);
            MyViewHolder holder = new MyViewHolder(view);

            return holder;
        }

        @Override
        public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {

            holder.checkBox.setText("Checkbox " + position);
            holder.checkBox.setChecked(imageModelArrayList.get(position).getSelected());
            holder.tvAnimal.setText(imageModelArrayList.get(position).getAnimal());
            alertviewlist =new ArrayList<ArrayList<Model>>();
            // holder.checkBox.setTag(R.integer.btnplusview, convertView);
            holder.checkBox.setTag(position);
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Integer pos = (Integer) holder.checkBox.getTag();
                    Toast.makeText(ctx, imageModelArrayList.get(pos).getAnimal() + " clicked!", Toast.LENGTH_SHORT).show();

                    //alertviewlist.addAll(imageModelArrayList);
                    for (int i = 0; i < imageModelArrayList.size(); i++) {
                        alertviewlist.add(i, imageModelArrayList);
                    }
                    System.out.println(alertviewlist.size());
                    if (imageModelArrayList.get(pos).getSelected()) {
                        imageModelArrayList.get(pos).setSelected(false);
                    } else {
                        imageModelArrayList.get(pos).setSelected(true);
                    }
                }
            });


        }

        @Override
        public int getItemCount() {
            return imageModelArrayList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            protected CheckBox checkBox;
            private TextView tvAnimal;

            public MyViewHolder(View itemView) {
                super(itemView);

                checkBox = (CheckBox) itemView.findViewById(R.id.cb);
                tvAnimal = (TextView) itemView.findViewById(R.id.animal);
            }

        }
    }

2 个答案:

答案 0 :(得分:0)

首先,您是否想将alertViewList作为列表项的列表?这是您的alertviewlist

alertviewlist = new ArrayList<ArrayList<Model>>();

根据您的描述,我认为您要制作的是一个列表,其中存储了模型吗?那么应该是:

alertviewlist = new ArrayList<Model>();

对于存储模型,您不需要将imageModelArrayList的列表添加到alertviewlist中。您只需将imageModelArrayList中具有给定pos的单个模型添加到alertviewlist中。在这里,我更改了您的checkBox.setOnClickListener的代码:

holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Integer pos = (Integer) holder.checkBox.getTag();
                Toast.makeText(ctx, imageModelArrayList.get(pos).getAnimal() + " clicked!", Toast.LENGTH_SHORT).show();

                //store your model into alertviewlist
                alertviewlist.add(imageModelArrayList.get(pos))

                System.out.println(alertviewlist.size());
                if (imageModelArrayList.get(pos).getSelected()) {
                    imageModelArrayList.get(pos).setSelected(false);
                } else {
                    imageModelArrayList.get(pos).setSelected(true);
                }
            }
        });

答案 1 :(得分:0)

CustomAdapter 类中

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

private LayoutInflater inflater;
public static ArrayList<Model> imageModelArrayList;
private Context ctx;


public CustomAdapter(Context ctx, ArrayList<Model> imageModelArrayList) {

    inflater = LayoutInflater.from(ctx);
    this.imageModelArrayList = imageModelArrayList;
    this.ctx = ctx;
}

@Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.rv_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
    final Model dResult = imageModelArrayList.get(position);
    holder.checkBox.setOnCheckedChangeListener(null);
    holder.checkBox.setChecked(dResult.getSelected());
    holder.checkBox.setTag(dResult);

    if (dResult.isSelected()) {
        holder.checkBox.setChecked(true);
        holder.checkBox.setSelected(true);
    } else {
        holder.checkBox.setChecked(false);
        holder.checkBox.setSelected(false);
    }

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                holder.checkBox.setSelected(true);
                dResult.setSelected(true);
            }else {
                holder.checkBox.setSelected(false);
                dResult.setSelected(false);
            }
        }
    });
}

@Override
public int getItemCount() {
    return imageModelArrayList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {

    protected CheckBox checkBox;
    private TextView tvAnimal;

    public MyViewHolder(View itemView) {
        super(itemView);

        checkBox = (CheckBox) itemView.findViewById(R.id.cb);
        tvAnimal = (TextView) itemView.findViewById(R.id.animal);
    }
}}

然后在您的活动班级中,将所选项目放入数组列表。

ArrayList<String> SelectedList = new ArrayList<String>();

for (int i=0;i<imageModelArrayList.size;i++){
        if (imageModelArrayList.get(i).getSelected()==true){
            SelectedList.add(imageModelArrayList.get(i).getAnimal());
        }
}

然后您可以将选定的动物名称添加到列表中。