我是 AlertDialogs 的新手,无法让它与光标一起使用。下面的代码在我的 onCreate() 函数中,我知道 Cursor 中有行。我是否缺少创建代码的一部分?
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();
光标生成器:
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;
}