SimpleCursorAdapter and ViewBinder - Binding data to ListView items to be retrieved on click

狂风中的少年 提交于 2019-12-01 22:19:26

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();      
    }    

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!