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

Answers


One simple solution is to 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 clickable.


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

from @ninjahoahong I add checkbox listenner in onBindViewHolder method and checkbox can click able.

viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int pos = viewHolder.getAdapterPosition();
            ChoiceItem currentItem = mChoice.get(pos);
            if(viewHolder.checkBox.isChecked()){
                onItemCheckListener.onItemCheck(currentItem);
                currentItem.setCheck(true);
            }else{
                onItemCheckListener.onItemUncheck(currentItem);
                currentItem.setCheck(false);
            }
        }
    });

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            List list= aList.get(position);
            //Toast.makeText(getApplicationContext(), movie.getTitle() + " is selected!", Toast.LENGTH_SHORT).show();
            action(list.getId());
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

Need Your Help

clang fails replacing a statement if it contains a macro

c++ clang clang++

I'm using clang to try and parse (with the C++ API) some C++ files and make all the case - break pairs use a specific style.

Which C/C++ header file defines a BYTE data type?

c++ c windows

I'm porting a header with this declaration: