Digvijay
Digvijay

Reputation: 3271

How to pass data from adapter class to bottom sheet dialog fragment

I have a recycler view in my fragment and every recycler view row item has a button so when user clicks on a button it should open BottomSheetDialogFragment and detail info of that particular row item should be visible in BottomSheetDialogFragment.

What I did so far dialog fragment is showing properly on the click of a button but I am not getting any idea of how can I pass data to dialog fragment from adapter class.

Below is my code:

FactsAdapter.java

public class FactsAdapter extends RecyclerView.Adapter<FactsAdapter.ViewHolder> {

List<Facts> factList;
Context context;

public FactsAdapter(List<Facts> factList, Context context) {
    this.factList = factList;
    this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.facts_row,parent,false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    final Facts model = factList.get(position);

    final String str1 = model.getDescription();

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.color.place);

    holder.title.setText(model.getTitle());

    Glide.with(context).load(model.getImage()).apply(requestOptions).into(holder.factImage);

    holder.more.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){ 
           
            BottomSheet bottomSheet = new BottomSheet();
            bottomSheet.show(((FragmentActivity) context).getSupportFragmentManager(),bottomSheet.getTag());
        }
    });

}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView factImage;
    TextView title;
    Button more;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

       factImage = itemView.findViewById(R.id.factImage);
       title = itemView.findViewById(R.id.title);
       more = itemView.findViewById(R.id.more);
      }
    }
}

How can I pass data to dialog fragment?

Upvotes: 4

Views: 4350

Answers (2)

Javad Dehban
Javad Dehban

Reputation: 1292

test it

adapter:

Bundle args = new Bundle();
args.putString("key", "value");
BottomSheet bottomSheet = new BottomSheet();  
bottomSheet .setArguments(args);
bottomSheet .show(((FragmentActivity) context).getSupportFragmentManager(),bottomSheet.getTag());

bottom sheet fragment :

Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");

Upvotes: 8

Roman Shtykalo
Roman Shtykalo

Reputation: 350

If I get you right, you can try to make one interface:

public interface DataTransferInterface {
     public void onSetValues(ArrayList<?> al);
}

Then impletent this inteface in your Fragment/Activity/BottomSheetDialogFragment and pass it as listener to your adapter's constructor:

@Override
public void onSetValues(List<Fact> list) {
   if (list.size() < 1) {
       txtEmptyAdapter.setVisibility(View.VISIBLE);
   }
}
        
public FactsAdapter(List<Facts> factList, Context context, DataTransferInterface listener) {
    this.factList = factList;
    this.context = context;
    this.listener = listener;
}

Now it's easy to call this listener wherever you need and get required data in your Fragment/Activity/BottomSheetDialogFragment, like that:

listener.onSetValues(factList);

Upvotes: 0

Related Questions