从 sqlite 数据库中检索数据并通过“OnItemClick"将其显示在另一个类中 [英] retrieving data from sqlite database and displaying it in another class through "OnItemClick"

查看:16
本文介绍了从 sqlite 数据库中检索数据并通过“OnItemClick"将其显示在另一个类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当我点击列表项时,应用程序应该加载另一个类,然后通过从 sqlite 数据库中检索数据来在 textview 上显示所需的数据

I want that as I click on list item the application should load another class and then display the required data on textview by retrieving the data from sqlite database

@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 类中

this code is in WordDetails class

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 没有得到正确的值?感谢您的帮助.

When I click on an item "Error" word is being displayed which is supposed to show, but wordTd equals -1. Why this wordId is not getting correct value? Thanks for your help.

推荐答案

放置 Intent 附加项时使用的键"区分大小写.您将 ID 设为...

The 'keys' used when putting Intent extras are case sensitive. You are putting the id as...

intent.putExtra("Word_ID", id);

...但是然后试图用...获得它

...but then trying to get it with...

wordId = getIntent().getIntExtra("WORD_ID", -1);

换句话说,您正在输入 Word_ID 并尝试获取 WORD_ID.更改代码,使它们相同并且应该可以工作.

In other words you're putting Word_ID and trying to get WORD_ID. Change the code so they're both the same and it should work.

这篇关于从 sqlite 数据库中检索数据并通过“OnItemClick"将其显示在另一个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆