1

In my activity, on click of a button I am opening a Dialog which has a RecyclerView in it.

final Dialog dialog = new Dialog(MyActivity.this);
dialog.setTitle("Details");
dialog.setContentView(R.layout.details_popup);
RecyclerView detailsRecyclerView = (RecyclerView) dialog.findViewById(R.id.detailsRecyclerView);
Button detailsCloseButton = (Button) dialog.findViewById(R.id.detailsCloseButton);

//feedbackResponseSubList contains n items

**DetailsAdapter detailsAdapter = new DetailsAdapter(R.layout.detail_item, feedbackResponseSubList);
detailsRecyclerView.setAdapter(detailsAdapter);**
detailsCloseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
dialog.show();

The close button is visible but the recyclerView is not getting populated with data. Even the methods onCreateViewHolder(), onBindViewHolder() and getItemCount() of Detailsdapter are not getting called.

Motish Jain
  • 71
  • 1
  • 6

3 Answers3

3

I have figured out the problem. After setting layout manager in recyclerview, it works fine.

LinearLayoutManager layoutManager = new LinearLayoutManager(dialog.getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        detailsRecyclerView.setLayoutManager(layoutManager);
Motish Jain
  • 71
  • 1
  • 6
1

The problem is that the Dialog is already created when you try to modify the adapter, because of that It's not getting populated with your data.

You have two options, create your own Dialog which extend Dialog class and in onCreate() put your code to populate the list or add adapter.notifyDataSetChanged() after setAdapter() to tell that your data inside the list was updated.

Miguel Benitez
  • 2,322
  • 10
  • 22
1

Use a dialog fragment instead of Dialog. An dialog fragment gives all the features of a dialog while providing the functionality of a fragment.

public class AlertDialogFragment extends DialogFragment {

     @Override
     public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      //inflate layout with recycler view
     View v = inflater.inflate(R.layout.details_popup, container, false);
     RecyclerView detailsRecyclerView = (RecyclerView) getActivity().findViewById(R.id.detailsRecyclerView);
     DetailsAdapter detailsAdapter = new DetailsAdapter(R.layout.detail_item, feedbackResponseSubList);
     detailsRecyclerView.setAdapter(detailsAdapter);
     detailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
     return v;
 }

 public Dialog onCreateDialog(Bundle SavedInstanceState) {
  //The rest of the code you will get when u search for dialogfragments
suku
  • 10,507
  • 16
  • 75
  • 120