自定义SimpleCursorAdapter,使用ImageButton随机值

3

我正在尝试为我的项目中的ListView添加图像按钮到自定义SimpleCursorAdapter,但是我在一个字段上遇到了重复和完全随机值的问题。

这是它的代码:

public class MyCursorAdapter extends SimpleCursorAdapter {
    public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        final Context t = context;
        final Cursor c = cursor;

        ImageButton delimageButton = (ImageButton)view.findViewById(R.id.deletebutton);

        delimageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               Toast.makeText(t,
                "Delete ID: " + c.getInt(c.getColumnIndex(MyDBAdapter.KEY_ID)), Toast.LENGTH_SHORT).show();

            }

        });

        if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_OWNID))>0)
        {   
            TextView own = (TextView)view.findViewById(R.id.ownInfo);
            own.setText("OWN");
        }
        else
        {
            TextView own = (TextView)view.findViewById(R.id.ownInfo);
            own.setText("");
        }
    }
}

现在,当我按下delimageButton时,我得到的是当前视图中ListView的某个记录(行)的随机ID(我可以看到它,但不是正确的ID)。例如,如果您在屏幕上看到5行,并按下其中一个按钮,则会获得其他行(这5行中的一行)的ID,而不是您按下的行(在大多数情况下)。我记得有些技巧可以使用自己的TextView,但我不知道它如何放在这里。
因此,请问如何使其显示正确的ID?
我将感激您的帮助。
编辑
以下是负责设置ListView并调用MyCursorAdapter的整个代码:
private void refreshList() {
        mySQLiteAdapter = new MyDBAdapter(this);
        mySQLiteAdapter.open();
        String[] columns = { MyDBAdapter.KEY_TITLE, MyDBAdapter.KEY_GENRE,
                MyDBAdapter.KEY_OWNID, MyDBAdapter.KEY_ID };
        Cursor contentRead = mySQLiteAdapter.getAllEntries(false, columns,
                null, null, null, null, MyDBAdapter.KEY_TITLE, null);
        startManagingCursor(contentRead);
        Log.d(TAG, Integer.toString(contentRead.getCount()));
        MyCursorAdapter adapterCursor = new MyCursorAdapter(this,
                R.layout.my_row, contentRead, columns, new int[] {
                        R.id.rowTitle, R.id.detail });
        this.setListAdapter(adapterCursor);
        mySQLiteAdapter.close();
    }

为了澄清,这个活动是ListActivity

1
你怎么知道这不是正确的ID?而且问题具体是什么? - mango
@mango 我知道,因为我正在使用SimpleCursorAdapter构造函数的值“from[]”和“to”在布局xml文件中打印它进行测试。感谢提醒我不要问不恰当的问题,我完全忘记了。 - sebap123
你在 ListView 上看到了一条记录(行)的随机 ID?但不是你点击的那一条,对吗?很抱歉,你的帖子很难理解。 - mango
你尝试过使用相同的ID从列表视图中删除记录吗?看看它是否会删除除所点击的内容以外的其他内容? - mango
那确实令人困惑。我们能看到适配器的实例化吗? - mango
显示剩余4条评论
1个回答

2
你的OnClickListener在点击时获取光标中的ID,而不是构建时获取。同时,ListView在滚动时更改光标位置。
如果你仔细看一下,你会发现Toast显示的是最后一个加载到视图中的项的ID,而不是包含你单击的按钮的项的ID。
你可以通过在构建单击监听器时获取ID来解决这个问题,像这样:
delimageButton.setOnClickListener(new OnClickListener() {
    private int id = c.getInt(c.getColumnIndex(MyDBAdapter.KEY_ID));
    @Override
    public void onClick(View arg0) {
        Toast.makeText(t,
            "Delete ID: " + id, Toast.LENGTH_SHORT).show();
    }
});

我知道这里有一些诀窍。非常感谢你。 - sebap123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接