【问题标题】:Why isn't my cursor working in my ListView?为什么我的光标在我的 ListView 中不起作用?
【发布时间】:2015-12-14 14:47:43
【问题描述】:

我想用在我的 ListView 上单击的项目替换我的 EditTexts。我使用光标沿着 ListView 上的项目移动。但是,即使我单击它上面的第二个项目,第一个项目也会显示出来。我做错了什么?

这是我的 ListView onItemClicked 代码:

lv_people_info.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override           
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            people_info2 = new ArrayList<String>();


            Cursor cursor2 = db.query(PeopleEntry.TABLE_NAME,// Table
                    allColumns, // The columns to return
                    null, // The columns for the WHERE clause
                    null, // The values for the WHERE clause
                    null, // don't group the rows
                    null, // don't filter by row groups
                    null, // The sort order
                    null); // Limits the number of rows returned by the  query  

            cursor2.moveToFirst();

            while (!cursor2.isAfterLast()) {
                people_info2.add(cursor2.getString(1));
                people_info2.add(cursor2.getString(2));
                people_info2.add(cursor2.getString(3));

                et_name.setText(people_info2.get(0));
                et_amount.setText(people_info2.get(1));
                et_description.setText(people_info2.get(2));

                cursor2.moveToNext();

            }

            cursor2.close();
        }

    });

当我单击 ListView 的第二行时会发生以下情况:

【问题讨论】:

  • 这段代码应该在任意行点击之后在表格中显示总是最后
  • @Selvin 实际上是第一个。视图是使用数组开头的内容设置的。
  • 是的,你是对的......重点是没有使用 int position, long id 传递给 onItemClick 回调
  • 使用setTag() 将主键绑定到ListView 行。在您的 ItemClickListener 中使用 getTag() 并使用该 ID 查询您的数据库。
  • @KNeerajLal 认真的......为了什么?不需要get/setTag 答案是错误的问题但是:stackoverflow.com/questions/33256083/filtering-on-a-list/…

标签: java android listview android-listview cursor


【解决方案1】:

我想用我的 ListView 上单击的项目替换我的 EditTexts。

嗯,这是你应该做的,而不是使用光标。你的列表视图已经被来自Cursor.的数据填充了,所以真的没有必要再次查询它!

只需在您的onClickListener() 中接收必要的信息即可。

您可以使用ViewHolder 模式。创建名为ViewHolder 的简单类,保留对您的姓名、金额、描述字段的引用。然后在您的 newView() 方法中创建新的 ViewHolder 并使用 setTag() 将其附加到您的视图。

bindview() 中绑定数据。然后,在OnClickListener 中使用来自viewgetTag()

你可以在这里看到很好的例子https://developer.android.com/training/improving-layouts/smooth-scrolling.html

不要在 UI 线程中查询您的数据库!在 OnClick 方法中查询数据库是错误的。如果你看完文章后还想查询数据库,至少用CursorLoader或者AsyncTask

【讨论】:

  • THERE IS NOED FOR OnClickListener as he is using ListView not RecyclerView ... 并且 ViewHolder 应该只保留对行中 (dums!!!) 视图的引用,而不是data ... 和 我想用我的 ListView 上单击的项目替换我的 EditTexts 。 prolly 意味着:我想将所选行中的数据绑定到 EditTexts
  • 嗨@RexSplode,我很抱歉,但我对此很陌生。那么在创建 ViewHolder 类之后,如何将我的 ListView 上点击的数据附加到 ViewHolder 中?
  • 数据在 bindview() 方法中附加到行。在您的 onClick() 中,您只需调用 getTag() 即可接收 ViewHolder 实例。之后,您可以调用 viewHolder.textView.getText() 以在单击的行中接收名称 textView 的文本。然后,editText.setText(retrivedString);
【解决方案2】:

下面的代码可以正常工作..

Inside Listview clickevent

private SQLiteDatabase myDataBase=myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
String sql=select name,amount,description from yourTableName 
Cursor cr = myDataBase.rawQuery(sql, null);

现在你在这里添加

 people_info2.add(cr .getString(1));
 people_info2.add(cr .getString(2));
 people_info2.add(cr .getString(3));

 et_name.setText(people_info2.get(0));
 et_amount.setText(people_info2.get(1));
 et_description.setText(people_info2.get(2));

【讨论】:

  • Programming by permutation 成就增益...这甚至不会编译,因为文字不在撇号中...同样db.rawQuerydb.query 相同,为什么它会有所帮助?跨度>
  • cr.getString 之间也有空格。这个答案的零努力
  • 俗称"Billion Monkeys Programming Style".
猜你喜欢
  • 2023-04-03
  • 2016-06-13
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多