-1

I have set data in RecyclerView on Fragment.I have one Button at the bottom below RecyclerView.After the Button is clicked I want to get some details from the individual card after Checkbox is checked.After the Button is clicked from Fragment I want to send the card details to Next Fragment using Bundles.
This is code for setting RecyclerView and with Button at Bottom

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="sam.sam.MyBook">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

    <Button
        android:id="@+id/assignTable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000"
        android:textColor="#fff"
        android:textSize="18sp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="center|bottom"
        android:layout_alignParentBottom="true"
        android:text="Assign"/>

</FrameLayout>  

This is code for Adapter

public class BookingAdapter extends RecyclerView.Adapter<BookingAdapter.ViewHolder> {
    private ArrayList<BookEntry> entry;
    Context context;
        String id;
    public BookingAdapter(ArrayList<BookEntry> entry, Context context) {
        this.entry = entry;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.booking_card, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final BookEntry currentEntry = entry.get(position);
        holder.name.setText(currentEntry.getName());
        holder.description.setText(currentEntry.getDescription());
        holder.capacity.setText(currentEntry.getCapacity());
        holder.tableId.setText(currentEntry.getId());
        holder.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id=entry.get(position).getId();
                final boolean isChecked=holder.check.isChecked();
                if(isChecked)
                {
                    Bundle bundle=new Bundle();
                    bundle.putString("tableIds",id);
                    TableAssignConfirm confirm=new TableAssignConfirm();
                    confirm.setArguments(bundle);
                }

            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, description, capacity,tableId;
    final     CheckBox check;
        public ViewHolder(final View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            description = (TextView) itemView.findViewById(R.id.description);
            capacity = (TextView) itemView.findViewById(R.id.capacity);
            tableId=(TextView)itemView.findViewById(R.id.tableId);
            check=(CheckBox)itemView.findViewById(R.id.book);
            check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isCheck=check.isChecked();
                    if(isCheck)
                        Log.i("isCheck","I am Check");
                        Log.i("id",id);
                }
            });
        }
    }
}  

How to do this ?

Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

1 Answers1

0

Lets assume for a moment that you can only have one card selected in your recycler view. Just add private int mSelectedCardIndex to your adapter as a member variable. onBinderViewHolder where you implement your click logic just onClick store the position into the mSelectedCardIndex. Also add a get method to your adapter so you can retrieve the data of the selected card:

public BookEntry getSelectedBookEntry(){
    return this.entry.get(mSelectedCardIndex);
}

How you will handle cases when there is no entries selected or multiple is up to you.

Then from your Fragment you can just call something like (on the button click of course):

BookEntry selectedBookEntry = recyclerView.getAdapter().getSelectedBookEntry();

followed by:

Bundle bundle=new Bundle();
bundle.putString(selectedBookEntry.getValueX());

and set args to the new detail fragment.

bko
  • 1,038
  • 1
  • 9
  • 17