首页 > 解决方案 > 如何获取在 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 但只需要列表中项目的值,如伏特加或威士忌

标签: javaandroidsqlite

解决方案


在您的 onclick 侦听器中:

cursor.moveToPosition(position);
String drinkString = cursor.getString(cursor.getColumnIndexOrThrow(" *column name* "));

推荐阅读