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

10,780

and yes i got the answer:

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;

    }
}
Share:
10,780
Pratik Jain
Author by

Pratik Jain

Updated on June 25, 2022

Comments

  • Pratik Jain
    Pratik Jain almost 2 years

    I am trying to select the checked items of the checkbox get the positions of item checked so as to send it to the server.

    My adapter is:

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