![](/img/trans.png)
[英]Retrieving Data from SQLite Database through AsyncTask and Displaying it
[英]retrieving data from sqlite database and displaying it in another class through “OnItemClick”
我希望當我單擊列表項時,應用程序應該加載另一個類,然后通過從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
沒有得到正確的價值? 謝謝你的幫助。
放置Intent
附加功能時使用的“鍵”區分大小寫。 你把id作為......
intent.putExtra("Word_ID", id);
......但是后來試着把它搞定......
wordId = getIntent().getIntExtra("WORD_ID", -1);
換句話說,你正在放置Word_ID
並嘗試獲取WORD_ID
。 更改代碼,使它們都相同,它應該工作。
該id
在默認情況下傳遞給OnItemClickListener是該行的從數據庫中唯一的ID,也沒有必要把它以這種方式:
id = wordList.getItemIdAtPosition(position);
將OnItemClickListener更改為:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(DictionaryActivity.this, WordDetails.class);
intent.putExtra("Word_ID", id);
startActivity(intent);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.