I try to do AlertDialog with ListView (adapter), and I get the Alert Dialog but I dont get the ListView, so my code is:

    public void namesDialog(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.list_view_names_dialog, null);
    final ListAdapterDetailsNames adapter = new ListAdapterDetailsNames(this, mNames.getNames());
    AlertDialog dialog = builder.create();


What I should do to get the listView in the AlertDialog?

Thanks a lot for you help :)

  • Your list view is empty. So then you see no items. – greenapps Feb 12 '18 at 15:37
  • What is your intention with mListViewTextNames? If it is a ListView then why not use that one? – greenapps Feb 12 '18 at 15:37
  • @greenapps the mListViewTextNames this is the ListView..what I should do to fix it? – A666 Feb 12 '18 at 15:41
  • That i already told you: builder.setView(mListViewTextNames); – greenapps Feb 12 '18 at 15:55
  • @greenapps i do this, but is not work.. – A666 Feb 12 '18 at 16:03

What is mListViewTextNames? You’re setting the adapter to a view that seems to exist even before you inflate R.layout.list_view_names_dialog... you almost certainly need to findViewById() within the inflated view to get the correct ListView to set the adapter on.

  • mListViewTextName is a ListView, so I try to get the ListView from layout: list_view_names_dialog – A666 Feb 12 '18 at 15:47
  • @Ei123456 do you see, though, that in the code you posted there's no reference to finding mListViewTextNames? You take the trouble to call inflate() on R.layout.list_view_names_dialog, and this is your dialog's body view, but since you're not actually obtaining a reference to a ListView from that body view, you must be setting the adapter on some other ListView that's not in the dialog. – Ben P. Feb 12 '18 at 16:16
  • ok, now I am understand this and it's work fine!, thanks @Ben P. – A666 Feb 12 '18 at 20:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.