【发布时间】:2012-07-02 19:38:53
【问题描述】:
我希望当我单击列表项时,应用程序应该加载另一个类,然后通过从 sqlite 数据库中检索数据来在 textview 上显示所需的数据
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(DictionaryActivity.this,
WordDetails.class);
// Cursor cursor = (Cursor) adapter.getItem(position);
id = wordList.getItemIdAtPosition(position);
intent.putExtra("Word_ID", id);
// cursor.getInt(cursor.getColumnIndex("_id"));
startActivity(intent);
}
此代码在 WordDetails 类中
wordId = getIntent().getIntExtra("WORD_ID", -1);
if (wordId == -1) {
mean = (TextView) findViewById(R.id.mean);
mean.setText("Error");
} else {
SQLiteDatabase db = (new DatabaseHelper(this))
.getReadableDatabase();
Cursor cursor = db.rawQuery(
"SELECT _id ,word, mean FROM Meanings WHERE _id = ?",
new String[] { "" + wordId });
if (cursor.getCount() == 1) {
cursor.moveToFirst();
mean = (TextView) findViewById(R.id.mean);
mean.setText(cursor.getString(cursor.getColumnIndex("mean")));
}
}
当我单击一个项目“错误”时,正在显示应该显示的单词,但 wordTd 等于 -1。为什么这个wordId 没有得到正确的值?感谢您的帮助。
【问题讨论】:
-
仅供参考,将代码剪切并粘贴到问题中后,再次突出显示代码并按 Ctrl+K 将其格式化为代码块。使用 ` 表示代码的 sn-ps。