SimpleCursorAdapter 和 ViewBinder - 将数据绑定到要在单击时检索的 ListView 项目
因此,我有一个从 SQLiteDatabase
填充的 ListView
(使用 ListActivity
)。我试图将行的 ID (PK) 附加到视图,以便每个列表项的 onListItemClick
,我可以使用该 ID 执行操作。
我读到可以使用 setTag
将任意数据设置为 View
并使用 getTag
检索(我实际上还没有成功完成这项工作然而,所以这可能是问题所在)。这是我正在使用的简化版本(为了简单/简洁):
public class Favorites extends ListActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FavoritesDB db = FavoritesDB.getInstance(this);
Cursor c = db.fetchFavorites();
startManagingCursor(c);
String[] columns = new String[] { "_id" };
int[] to = new int[] { R.id.word };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.favorite, c, columns, to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
view.setTag(cursor.getInt(0));
return true;
}
});
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Object wordID = v.getTag();
Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();
}
}
正在填充 ListView
,并且 Toast
确实显示,但它始终是 "ID=null"
,所以显然 ID 没有在 ViewBinder
调用 setTag
中设置(或者没有通过以下方式检索属性) getTag
)。
So I've got a ListView
(using a ListActivity
) that I'm populating from a SQLiteDatabase
. I'm trying to attach the ID (PK) of the row to the view, so that onListItemClick
of each list item, I can do stuff with that ID.
I've read that arbitrary data can be set to a View
using setTag
and retrieved with getTag
(I haven't actually had this work successfully yet, so this may be the problem). Here's a pared down version of what I'm using (for simplicity/brevity):
public class Favorites extends ListActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FavoritesDB db = FavoritesDB.getInstance(this);
Cursor c = db.fetchFavorites();
startManagingCursor(c);
String[] columns = new String[] { "_id" };
int[] to = new int[] { R.id.word };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.favorite, c, columns, to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
view.setTag(cursor.getInt(0));
return true;
}
});
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Object wordID = v.getTag();
Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();
}
}
The ListView
is being populated, and the Toast
does show up, but it's always "ID=null"
, so apparently the ID isn't being set in the ViewBinder
call to setTag
(or isn't being retrieved property with getTag
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您的
R.layout.favorite
实现。如果您的布局包含一个带有子 TextView 的父视图,例如您设置的标签是针对 TextView 的,而从onListItemClick()
接收到的 View v 是父视图。您需要确保收到您设置的同一视图的标签,方法是:This depends on your implementation of
R.layout.favorite
. If you have this layout contains a parent view with child TextViews for e.g. the tag you set is for the TextViews while the View v received from theonListItemClick()
is the parent View. You need to make sure that you receive the tag for the same view you set by using:您可能应该从适配器获取光标。这样,如果您的光标被替换,您仍然可以获得有效的光标。
注意:
您的适配器必须声明为
SimpleCursorAdapter
否则您应该向下转换它。You probably should get the cursor from the adapter. This way if your cursor gets replaced you are still are still getting a valid cursor.
NOTE :
your adapter must be declared as a
SimpleCursorAdapter
other wise you should downcast it.