这是我的数据库查询和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* "));

关于java - 如何获取Android中 ListView 中单击的项目的字符串值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56764733/

10-09 23:14