I’m new to AlertDialogs and cannot get it to work with Cursors. The code below is in my onCreate() function and I know the Cursor has rows in it. Am I missing part of the creation code?
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Choose a playlist");
// set dialog message
alertDialogBuilder.setCancelable(false);
// Add cursor items
alertDialogBuilder.setCursor(cursor, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int pos) {
Toast.makeText(getApplicationContext(), "Clicked on: " + pos, Toast.LENGTH_SHORT).show();
}
}, MediaStore.Audio.Media.DISPLAY_NAME);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
Cursor Builder:
private Cursor getPlaylists() {
String[] ARG_STRING = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.IS_MUSIC
};
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
ARG_STRING,
null,
null,
null);
int nameColumn = cursor.getColumnIndex(MediaStore.Audio.Media.IS_NOTIFICATION);
cursor.moveToFirst();
for (int i=0; i<5; i++)
Toast.makeText(getApplicationContext(), "" + cursor.getString(nameColumn), Toast.LENGTH_SHORT).show();
return cursor;
}
You are right. I meant the code to look as follows:
That should work. Can you show your logcat if it doesn’t?