我只是想让用户点击的行中的文本显示在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();
相反,我不断得到:
我做错了什么?
答案 0 :(得分:2)
方法getItemAtPosition()
正在返回一个SQLiteCursor
对象,这种对象没有方法toString()
的实现。因此,在这种情况下,您必须返回光标本身并在调用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();