Search code examples
androidlistviewandroid-activitylistactivitycustomdialog

Listview and dialog in android


I have a small custom dialog in wich I have a ListView with id:list. I want to populate it with strings I have in my resources (R.array.tones), but I have really big problems making this work, have tried many different solutions this is the latest which I think would work but it throws an null pointer exception on the toneList.

    Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.tone_dialog);
    dialog.setTitle(R.string.tonePromptTitle);
    ListView toneList = (ListView)findViewById(R.id.list);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tones, android.R.layout.simple_list_item_1);

    toneList.setAdapter(adapter);

    dialog.show();

My class is just extending Activity not ListActivity, and I would like to have it that way otherwise I must create a new class just for the listview. I apologize for the long code, I'm new here and hasn't really figured out all functionality yet.


Solution

  • You should look for the View inside of the dialog:

     ListView toneList = (ListView)dialog.findViewById(R.id.list);
    

    :)