Glenn Arkell
Glenn Arkell

Reputation: 111

AlertDialog with a Cursor

I am looking for some help in understanding the workings of the Alert Dialog. I currently have a working dialog that retrieves a listing of players from the SQLite database. The idea is for the user to select a listed player from the list and I store that name in a variable. The snipet of code below gives me the position integer of the name.

return new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle("Choose a Player")
.setSingleChoiceItems(dba.getAllPlayers(), -1, Constants.playerName, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {

***** get the name of the player selected ****
dialog.dismiss();
startMenu();
}
})
.create();

I need the syntax to reference a cursor. I understand the more basic listing of an array and referencing the selected item from that array (items[item]) as per the doco (http://developer.android.com/guide/topics/ui/dialogs.html), but how do I reference the listing from my call to the database?

Tried playerName = dba.getAllPlayers().getString(item); but I get a "CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1" type error.

Thanks in advance and hope someone might shed some light on this for me. Cheers.

Glenn Aging Cobol Programmer Very New to Android

Upvotes: 1

Views: 2379

Answers (1)

Cristian
Cristian

Reputation: 200150

If your do:

playerName = dba.getAllPlayers().getString(item);

You are telling android to search the string in the column number item from the Cursor. This of course makes no sense at all. What you need is to ask for the position, thus this looks better:

final Cursor cursor = dba.getAllPlayers()
.setSingleChoiceItems(cursor, -1, Constants.playerName, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {

cursor.moveToPosition(item);
String blah = cursor.getString(cursor.getColumnIndex(Constants.playerName));

Upvotes: 2

Related Questions