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

标签 android database listview

我在 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/

相关文章:

除非您触摸 map View ,否则不会加载 Android MapView

android - setValue() 没有将数据保存到我的实时数据库

android - 如何删除 ListView 中选中的项目

Android:如何像ListView一样用一个TextView快速滚动ScrollView?

android - 没有收到带有 smack 的离线消息

java - Android 中的双击缩放

php - 无法从服务器 2 中的 Apache 连接到服务器 1 上的 MySQL 实例

database - 客观地从 Oracle 数据库中删除数据

mysql - 如何从两个表中获取同义词?

java - 按字母顺序对 ListView 的元素进行排序