我正在尝试为我的项目中的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
。
ListView
上看到了一条记录(行)的随机 ID?但不是你点击的那一条,对吗?很抱歉,你的帖子很难理解。 - mango