vendredi 30 octobre 2015

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




Aucun commentaire:

Enregistrer un commentaire