2012-01-14 30 views
1

我在寻找一些帮助来理解警报对话框的工作。我目前有一个工作对话框,用于从SQLite数据库中检索玩家列表。这个想法是用户从列表中选择列出的播放器,并将该名称存储在变量中。下面的代码snipet给了我名字的位置整数。带光标的AlertDialog

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(); 

我需要语法来引用游标。根据doco(http://developer.android.com/guide/topics/ui/dialogs.html),我理解数组的更基本列表并引用该数组中选定的项目(items [item]),但我如何参考我的电话到数据库的清单?

尝试playerName = dba.getAllPlayers().getString(item);但我得到一个“CursorIndexOutOfBoundsException:索引-1请求,大小为1”类型的错误。

在此先感谢,希望有人可以为我解释这一点。干杯。

格伦 老化的Cobol程序员 很新到Android

回答

2

如果你做:

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

你告诉机器人从Cursor搜索字符串中的列数item。这当然是毫无意义的。你需要的是要求的位置,因此这看起来更好:

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)); 
+0

神奇,卓越,辉煌。你是我的Android大师。谢谢Cristian。 – user1148731 2012-01-14 00:58:05