无法从 ListView 获取选定的项目文本
Can't get selected item text from ListView
我只是想在用户单击的行的 ListView 中显示文本。
列表视图和适配器
ListView listCurrent = (ListView)findViewById(R.id.currentlinkslist);
try {
SQLiteOpenHelper tractionDatabaseHelper = new TractionDatabaseHelper(this);
db = tractionDatabaseHelper.getReadableDatabase();
String query = "SELECT _id, QUESTION FROM QUESTION";
cursor = db.rawQuery(query, null);
CursorAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"QUESTION"},
new int[]{android.R.id.text1},0);
listCurrent.setAdapter(listAdapter);
}catch(SQLiteException e) {
Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT).show();
}
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listCurrent,
View v, int position, long id) {
String selectedItem = listCurrent.getItemAtPosition(position).toString();
Toast.makeText(AssociateActivity.this,
selectedItem , Toast.LENGTH_SHORT).show();
}
};
listCurrent.setOnItemClickListener(itemClickListener);
根据我所见,AdapterView.OnItemClickListener 中的这段代码应该获取描述:
String selectedItem = listCurrent.getItemAtPosition(position).toString();
Toast.makeText(AssociateActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
相反,我不断得到:
我做错了什么?
方法 getItemAtPosition()
是 returning 一个 SQLiteCursor
对象,这种对象没有方法 toString()
的实现。因此,在这种情况下,您必须 return 游标本身并将其解析为您的对象,然后再调用 toString()
方法。
SQLiteCursor cursor = (SQLiteCursor) listCurrent.getItemAtPosition(position);
String selectedItem = cursor.getString(columIndex); //columIndex is not the position
Toast.makeText(AssociateActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
我只是想在用户单击的行的 ListView 中显示文本。
列表视图和适配器
ListView listCurrent = (ListView)findViewById(R.id.currentlinkslist);
try {
SQLiteOpenHelper tractionDatabaseHelper = new TractionDatabaseHelper(this);
db = tractionDatabaseHelper.getReadableDatabase();
String query = "SELECT _id, QUESTION FROM QUESTION";
cursor = db.rawQuery(query, null);
CursorAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"QUESTION"},
new int[]{android.R.id.text1},0);
listCurrent.setAdapter(listAdapter);
}catch(SQLiteException e) {
Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT).show();
}
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listCurrent,
View v, int position, long id) {
String selectedItem = listCurrent.getItemAtPosition(position).toString();
Toast.makeText(AssociateActivity.this,
selectedItem , Toast.LENGTH_SHORT).show();
}
};
listCurrent.setOnItemClickListener(itemClickListener);
根据我所见,AdapterView.OnItemClickListener 中的这段代码应该获取描述:
String selectedItem = listCurrent.getItemAtPosition(position).toString();
Toast.makeText(AssociateActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
相反,我不断得到:
我做错了什么?
方法 getItemAtPosition()
是 returning 一个 SQLiteCursor
对象,这种对象没有方法 toString()
的实现。因此,在这种情况下,您必须 return 游标本身并将其解析为您的对象,然后再调用 toString()
方法。
SQLiteCursor cursor = (SQLiteCursor) listCurrent.getItemAtPosition(position);
String selectedItem = cursor.getString(columIndex); //columIndex is not the position
Toast.makeText(AssociateActivity.this, selectedItem, Toast.LENGTH_SHORT).show();