gpt4 book ai didi

android - 带光标的 AlertDialog

转载 作者:行者123 更新时间:2023-11-29 22:14:11 24 4
gpt4 key购买 nike

我正在寻求帮助以了解警报对话框的工作原理。我目前有一个工作对话框,可以从 SQLite 数据库中检索玩家列表。这个想法是让用户从列表中选择一个列出的玩家,然后我将该名称存储在一个变量中。下面的代码 fragment 为我提供了名称的位置整数。

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 新手

最佳答案

如果你这样做:

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

您要告诉 android 从 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));

关于android - 带光标的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8859039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com