【发布时间】: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