java - 如何获取在 Android 的列表视图中单击的项目的字符串值
问题描述
这是我的数据库查询和 SimpleCursorAdapter 设置:
db = stockDatabaseHelper.getReadableDatabase();
final Cursor cursor = db.rawQuery("SELECT _id, name FROM types", null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
cursor,
new String[] { StockDatabaseContract.StockType.COLUMN_NAME_NAME },
new int[] { android.R.id.text1 },
0);
lvTypes.setAdapter(listAdapter);
这是点击代码:
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(TypeSelection.this, BrandSelection.class);
String selectedType = parent.getItemAtPosition(position).toString();
intent.putExtra(BrandSelection.TYPE_NAME, selectedType);
startActivity(intent);
}
};
我似乎只得到一个值,如 android.database.sqlite.SQLiteCursor@435b9ba0 但只需要列表中项目的值,如伏特加或威士忌
解决方案
在您的 onclick 侦听器中:
cursor.moveToPosition(position);
String drinkString = cursor.getString(cursor.getColumnIndexOrThrow(" *column name* "));
推荐阅读
- python - 如何从自定义 COCO 数据集中保存图像,其注释覆盖在图像之上
- python - 现场直播 django KeyError,使用 pycharm 在车站上工作
- powershell - 本地 Azure 函数自动关闭
- reactjs - “Decorators-legacy”当前未启用
- latex - 如何在 RMarkdown 文件中使用 LaTeX 考试类显示解决方案?
- cmake - CMake 复制 dll 传递
- python - R语言中带有分位数的滚动平均值
- bash - 在变量中设置字符串的值
- python - 雪花:在 SQL 过程中出现内存错误
- java - 如何从 recyclerview newsapi 加载 url