SimpleCursorAdapter 和 ViewBinder - 将数据绑定到要在单击时检索的 ListView 项目

发布于 2024-12-19 13:47:13 字数 1560 浏览 2 评论 0原文

因此,我有一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-12-26 13:47:13

这取决于您的 R.layout.favorite 实现。如果您的布局包含一个带有子 TextView 的父视图,例如您设置的标签是针对 TextView 的,而从 onListItemClick() 接收到的 View v 是父视图。您需要确保收到您设置的同一视图的标签,方法是:

    @Override      
    protected void onListItemClick(ListView l, View v, int position, long id) {
    Object wordID = v.getChild(0).getTag();          
    Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();      
    }    

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 the onListItemClick() is the parent View. You need to make sure that you receive the tag for the same view you set by using:

    @Override      
    protected void onListItemClick(ListView l, View v, int position, long id) {
    Object wordID = v.getChild(0).getTag();          
    Toast.makeText(getBaseContext(), "ID=" + wordID, 1).show();      
    }    
套路撩心 2024-12-26 13:47:13

您可能应该从适配器获取光标。这样,如果您的光标被替换,您仍然可以获得有效的光标。

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       Cursor cursor =  adapter.getCursor();
       cursor.moveToPosition(position);
       String id = cursor.getString(cursor.getColumnIndex("primary key field name in database");
       Toast.makeText(getBaseContext(), "ID=" + id, 1).show();
    } 

注意:
您的适配器必须声明为 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.

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       Cursor cursor =  adapter.getCursor();
       cursor.moveToPosition(position);
       String id = cursor.getString(cursor.getColumnIndex("primary key field name in database");
       Toast.makeText(getBaseContext(), "ID=" + id, 1).show();
    } 

NOTE :
your adapter must be declared as a SimpleCursorAdapter other wise you should downcast it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文