Search code examples
androidlistviewcustomdialog

ListView with Custom dialog box


I have written the code in which I am getting the result from database which I am binding to listview successfully.But Now I want that listview in Custom dialog format.Means List data should come in dialog.How to attach the listdata to custom dialog box ?

final Cursor cursor = dbHelper.fetchAllRecords();
        String[] columns = new String[] {
                RecordsDbAdapter.KEY_NAME,
                RecordsDbAdapter.KEY_BIRTHDAY,

        };
        int[] to = new int[] {
                R.id.name,
                R.id.birthdate,
        };
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.row,
                cursor,
                columns,
                to);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(dataAdapter);

Solution

  • You can bind an adapter to an AlertDialog:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setAdapter(dataAdapter, new DialogInterface.OnClickListener() {
    
        @Override
        public void onClick(DialogInterface dialog, int which) {
    
            // Stuff that happens when an item is clicked
        }
    });
    builder.show();