【问题标题】:Custom SimpleCursorAdapter with imageButton random values具有 imageButton 随机值的自定义 SimpleCursorAdapter
【发布时间】:2013-01-05 00:14:32
【问题描述】:

我正在尝试为我的项目中的 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 行并且您按下其中一个按钮,您将获得另一行的 id(这 5 行之一),但不是您按下的这一行(在大多数情况下)。我记得这个自己的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

【问题讨论】:

  • 你怎么知道它不是正确的id?究竟是什么问题?
  • @mango 我知道,因为我正在使用 SimpleCursorAdapter 构造函数的值 from[]to 在布局 xml 文件中将其打印出来以进行测试。感谢您提醒我没有提出正确的问题,我完全失去了它。
  • 您在ListView 上看到一条记录(行)的随机ID?但不是你点击的那个,对吗?很抱歉,您的帖子很难理解。
  • @mango 是的,你是对的。我认为这很容易理解。我会尝试重写它。
  • 您是否尝试过从列表视图中删除具有相同 ID 的记录?看看它是否删除了点击之外的东西?

标签: java android android-listview android-cursoradapter android-cursor


【解决方案1】:

您的 OnClickListener 在单击时从光标获取 id,而不是在构造时。同时,当您滚动时,列表视图正在更改光标位置。

我想如果你仔细看,你会发现 Toast 显示的是最后一个加载到视图中的项目的 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();
    }
});

【讨论】:

  • 我知道这有一些技巧。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2012-08-31
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
相关资源
最近更新 更多