bircastri
bircastri

Reputation: 2169

How can show a Dialog with RecycleView

I have an Android activity with a RecycleView and I have implemented a ClickEvent on this RecycleView.

If I try to click on one items, I want to display a Dialog with another RecycleView.

So this is my activity code:

public class ResultActivity extends AppCompatActivity {
    private List<Result> lista= new ArrayList<Result>();
    private RecyclerView recyclerView;
    private RecyclerView recyclerViewResult;
    private ResultsAdapter pAdapter;
    private ResultXResultAdapter prAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try{


        super.onCreate(savedInstanceState);
        setContentView(R.layout.results_activity);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        ResultDAO manager = new ResultDAO(this);
        lista=manager.getResults();

        pAdapter = new ResultsAdapter(lista, new ResultsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Result item) {

                    try{
                        final Dialog dialog = new Dialog(ResultActivity.this);

                        dialog.setContentView(R.layout.result_modal);
                        recyclerViewResult = (RecyclerView) findViewById(R.id.recycler_result_view);
                        dialog.setTitle("Parametri");

                        prAdapter = new ResultXResultAdapter(item.getListaParametri());
                        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                        recyclerViewResult.setLayoutManager(mLayoutManager);
                        recyclerViewResult.setItemAnimator(new DefaultItemAnimator());
                        recyclerViewResult.setAdapter(prAdapter);


                        dialog.show();
                    }catch(Exception e){
                        Log.e("","");
                    }
            }
        });
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(pAdapter);
       // prepareMovieData();
        }catch(Exception e){
        }
    }
}

If I run my application and I try to click on one items, I have null exception. The problem is in this line code:

recyclerViewResult = (RecyclerView) findViewById(R.id.recycler_result_view);

after this code, the recyclerViewResult is null, but should not be null.

Upvotes: 0

Views: 837

Answers (1)

Pztar
Pztar

Reputation: 4759

The reason your RecyclerView is returning null is because you're calling findViewById without the correct view prefix. Because you're using a custom layout in your Dialog you should use a LayoutInflater to inflate the layout then use the inflated view object to find the RecyclerView that belongs in the dialog like so:

View dialogView = inflater.inflate(R.layout.result_modal, null); recyclerViewResult = (RecyclerView) dialogView.findViewById(R.id.recycler_result_view) dialog.setContentView(dialogView)

Upvotes: 2

Related Questions