Get list of checked checkboxes from recyclerview android

I have populated the recyclerView with image, title and checkbox.
I have two problems.

  1. How to make the checkbox selected when the imageview or the whole recycler item is clicked.

  2. Drawer indicator in lollipop play store
  3. How can port AsyncTask from Android to java?
  4. Check that getArguments has some data to be retrieved
  5. install X509 certificate programmatically in my case
  6. Picasso load image into GifImageView (android-gif-drawable library)
  7. Phonegap app dies in background while collecting location
  8. I have to go to next activity by getting all the checked items from the recyclerview.

My layout :

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center_horizontal"
            android:contentDescription="Interests"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_yash_dp" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_gravity="bottom"
            android:layout_margin="5dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/rounded_corners"
            android:gravity="bottom"
            android:padding="5dp"
            android:text="GYM"
            android:textAlignment="center"
            android:textColor="@color/white" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/checkBox"
            android:layout_margin="2dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

My adapter:

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
final InterestBean model = arrayList.get(position);
final int pos = position;

RecyclerViewHolder mainHolder = (RecyclerViewHolder) holder;// holder

Bitmap image = BitmapFactory.decodeResource(context.getResources(),
        model.getImage());// This will convert drawbale image into bitmap

// setting title
mainHolder.title.setText(model.getTitle());
mainHolder.imageview.setImageBitmap(image);
mainHolder.checkBox.setChecked(arrayList.get(position).isSelected());
mainHolder.checkBox.setTag(arrayList.get(position));


mainHolder.checkBox.setOnClickListener(new View.OnClickListener()     {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
InterestBean contact = (InterestBean) cb.getTag();

contact.setIsSelected(cb.isChecked());
arrayList.get(pos).setIsSelected(cb.isChecked());
selectedItems.add(pos);
Toast.makeText(
v.getContext(), pos + cb.isChecked(), Toast.LENGTH_LONG).show();
}
});}

Related posts:

How to count speed based on steps
What is the use of &lt;permission-group&gt; in android?
android disabling predictive text
How to add padding for background image
How to debug signed Android app from Eclipse?
Cordova: Is it possible to have different app IDs for android and ios platforms?
  • Using LibPhoneNumber google lib in Android
  • PopupMenu equivalent in ActionBarSherlock
  • Why does my OnLocationChanged() never get called?
  • Menu ISSUES with some Android 4.x devices
  • Which Android devices support jni?
  • Border Color for Editor in Xamarin.Forms
  • 3 Solutions collect form web for “Get list of checked checkboxes from recyclerview android”

    One simple solution can be that you can create OnItemCheckLister, and implement it similar to following:

    public class MyAdapter extends RecyclerViewAdapter {
    
        interface OnItemCheckListener {
            void onItemCheck(Item item);
            void onItemUncheck(Item item);
        }
    
        ...
    
        @NonNull
        private OnItemCheckListener onItemCheckListener;
    
        public MyAdapter (List<Item> items, @NonNull OnItemCheckListener onItemCheckListener) {
            this.items = items;
            this.onItemClick = onItemCheckListener;
        }
    
        ...
    
        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof MyViewHolder) {
                final Item currentItem = items.get(position);
    
                ...
    
                ((MyViewHolder) holder).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((MyViewHolder) holder).checkbox.setChecked(
                                !((MyViewHolder) holder).checkbox.isChecked());
                        if (((MyViewHolder) holder).checkbox.isChecked()) {
                            onItemClick.onItemCheck(currentItem);
                        } else {
                            onItemClick.onItemUncheck(currentItem);
                        }
                    }
                });
            }
        }
    
        static class MyViewHolder extends RecyclerView.ViewHolder {
            CheckBox checkbox;
            View itemView;
    
            ...
    
            public MyViewHolder(View itemView) {
                super(itemView);
                this.itemView = itemView;
                checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
                checkbox.setClickable(false);
                ...
            }
    
            public void setOnClickListener(View.OnClickListener onClickListener) {
                itemView.setOnClickListener(onClickListener);
            }
        }
    }
    

    Then in the activity, we can do this:

        private List<Item> currentSelectedItems = new ArrayList<>();
    
        ...
    
        myAdapter = new MyAdapter(items, new MyAdapter.OnItemCheckListener() {
                @Override
                public void onItemCheck(Item item) {
                    currentSelectedItems.add(item);
                }
    
                @Override
                public void onItemUncheck(Item item) {
                    currentSelectedItems.remove(item);
                }
            });
    

    Then you can do stuff with currentSelectedItems.

    Note: Since this solution suppose for the whole Item to be pressed so the checkbox is set to be not clicked able.

    You can use Model class to get recyclerView checked items. More reference : Android Recyclerview checkbox

    Model

    public class Model {
    
        private boolean isSelected;
        private String animal;
    
        public String getAnimal() {
            return animal;
        }
    
        public void setAnimal(String animal) {
            this.animal = animal;
        }
    
        public boolean getSelected() {
            return isSelected;
        }
    
        public void setSelected(boolean selected) {
            isSelected = selected;
        }
    }
    

    Adapter

    import android.content.Context;
     import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
        import android.view.View;
     import android.view.ViewGroup;
     import android.widget.CheckBox;
     import android.widget.TextView;
     import android.widget.Toast;
    
     import java.util.ArrayList;
    
    
      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) {
    
        holder.checkBox.setText("Checkbox " + position);
        holder.checkBox.setChecked(imageModelArrayList.get(position).getSelected());
        holder.tvAnimal.setText(imageModelArrayList.get(position).getAnimal());
    
       // 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();
    
                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);
        }
    
    }
    

    1) You can add mainHolder.imageview OnClickListener
    2) Two step:

    1. Add to InterestBean model boolen isChecked
    2. Where go to next activity, get all InterestBeans, get all checked InterestBeans and save it.

    Code:

    mainHolder.imageview.setOnClickListener(new View.OnClickListener()
        {
            public void onClick (View v)
            {
                mainHolder.checkBox.setChecked(!mainHolder.checkBox.isChecked());
            }
        }
        );
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.