Android: 如何获取RecyclerView中checkbox的勾选项发送到服务器

Android: How to get the Checked items of checkbox in RecyclerView to send it to server

我正在尝试 select checkbox 的已检查项目获取已检查项目的位置,以便将其发送到服务器。

我的适配器是:

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

    ArrayList<String> topics_title, topics_id;
    ArrayList<String> checked_items= new ArrayList<>();
    Context context;
    LinearLayout linearLayout;
    CheckBox checkBox;

    public FollowTopicsAdapter(Context context, ArrayList<String> topics_title, ArrayList<String> topics_id){
        this.topics_title=topics_title;
        this.context=context;
        this.topics_id=topics_id;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_for_follow_topics,parent,false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.checkBox.setText(topics_title.get(position));
        holder.checkBox.setTag(topics_title.get(position));

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    checked_items.add(position,topics_id.get(position));
                    holder.checkBox.setChecked(b);
                    linearLayout.setBackgroundResource(R.drawable.checked);
                }
                else
                {
                    checked_items.remove(position);
                    holder.checkBox.setChecked(b);
                    linearLayout.setBackgroundResource(R.drawable.custom_checkbox);
                }

            }
        });
    }


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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;

        public MyViewHolder(View view) {
            super(view);
            linearLayout=(LinearLayout)itemView.findViewById(R.id.linearLayout);
            checkBox= (CheckBox)itemView.findViewById(R.id.checkbox);
        }
    }
}

你可以在这里看到我的代码

https://github.com/masaliev/android-dynamic-table

。我希望你能理解它是如何完成的。

是的,我得到了答案:

public class AdapterForFollowTopics extends BaseAdapter {

    private Context context;
    ArrayList<String> topics_title, topics_id;
    ArrayList<String> checked_items= new ArrayList<>();
    private static LayoutInflater inflater=null;
    ArrayList<String> lstChk= new ArrayList<>();

    public AdapterForFollowTopics(Context context, ArrayList<String> topics_title, ArrayList<String> topics_id){
        this.context = context;
        this.topics_title = topics_title;
        this.topics_id= topics_id;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int i) {
        return topics_title.get(i);
    }

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

    public class Holder
    {
        CheckBox check;
        LinearLayout lv;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final Holder holder = new Holder();
        final View rowView;

        rowView = inflater.inflate(R.layout.layout_for_follow_topics, null);
        holder.check = (CheckBox) rowView.findViewById(R.id.checkbox);
        holder.lv = (LinearLayout) rowView.findViewById(R.id.linearLayout);
        holder.check.setText(topics_title.get(position));
        rowView.setTag(holder);

        if(lstChk.contains(topics_title.get(position)))
        {
            holder.check.setChecked(true);
        }

        holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked())
                {
                    compoundButton.setChecked(true);
                    lstChk.add(topics_title.get(position));
                    holder.lv.setBackgroundResource(R.drawable.checked);

                }
                else
                {
                    compoundButton.setChecked(false);
                    lstChk.remove(topics_title.get(position));
                    holder.lv.setBackgroundResource(R.drawable.custom_checkbox);
                }
            }
        });

        return rowView;

    }
}