gpt4 book ai didi

android - 单击 listView 从数据库访问数据

转载 作者:搜寻专家 更新时间:2023-10-30 23:21:01 25 4
gpt4 key购买 nike

我在 activity1 中有一个 ListView,其中有一些项目是来自数据库的单词。用户单击 Listview 中的一个项目,它将导航到 activity2,在那里它应该显示存储在数据库中的单词的详细信息作为列 word, definitions 。但是 Screen2 中出现的内容取决于在 Screen 1 中单击的项目

对于 Ex - 用户在屏幕 1 中单击 A - 从 A 开始的单词出现在屏幕 2 中。是否有任何方法可以传递行 ID,以便在下一个屏幕中可以显示数据库中的单词和定义。

谢谢你的进..第一个 Activity 的代码:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub


Cursor c = mDictCursor;
c.moveToPosition(position);
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle=new Bundle();
//intent.putExtra("position",position);
bundle.putLong(DBAdapter.KEY_ROWID, id);
bundle.putString(DBAdapter.KEY_TITLE, c.getString(
c.getColumnIndexOrThrow(DBAdapter.KEY_TITLE)));
bundle.putString(DBAdapter.KEY_DEFINITION, c.getString(
c.getColumnIndexOrThrow(DBAdapter.KEY_DEFINITION)));
i.putExtras(bundle);
startActivity(i);
}

SecondActivity代码:

Bundle extras = getIntent().getExtras();

        mRowId = extras.getLong(DBAdapter.KEY_ROWID);
String title = extras.getString(DBAdapter.KEY_TITLE);
String body = extras.getString(DBAdapter.KEY_DEFINITION);


TextView word = (TextView) findViewById(R.id.word);
word.setText(title);

TextView definition = (TextView) findViewById(R.id.definition);
definition.setText(body);
}

每当我单击 ListView 项目时,它都会显示强制关闭的对话框。请帮助...

最佳答案

您可以在 Screen1 中使用 Intent 对象的 putExtra(String name, int value) 方法(见 1)并将 intent 对象传递给 Screen2,在 Screen2 中使用 Intent 对象的 getIntExtra(String name, int defaultValue) 方法(见 2)。1.在Screen1中使用startActivity(Intent intent)方法2.在 Screen2 中使用 getIntent() 方法获取您在 Screen1 中传递的 Intent 对象

关于android - 单击 listView 从数据库访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6558323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com