我有一个使用自定义cursoradaptor的sqldatabase列表视图。我想回到我在ListView中单击项时用来创建项的活动,以便编辑项。但是,当我在cursoradapter上实现onitemclick方法和getitemid()时,什么也不会发生,即使我不确定我是否正确。这是我的代码:
public void onItemClick(AdapterView<?> adapview, View view, int position, long rowId) {
Cursor c = adapter.retrieveRow(rowId); // retrieve row from Database
Intent edit = new Intent(this,NewItem.class);
edit.putExtra(DBAdapter.KEY_ID, rowId);
edit.putExtra(DBAdapter.Title, c.getString(c.getColumnIndex(DBAdapter.Title)));
edit.putExtra(DBAdapter.DATE, c.getString(c.getColumnIndex(DBAdapter.DATE)));
startActivity(edit);
}
public long getItemId(int id){
return id;
}
最佳答案
我认为您需要在listview上设置一个onitemclicklistener,而不是尝试在adapter类中实现它。
例子:
mListView = (ListView)findViewbyId(R.id.whatever)
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long rowId) {
Cursor c = adapter.retrieveRow(rowId); // retrieve row from Database
Intent edit = new Intent(this,NewItem.class);
edit.putExtra(DBAdapter.KEY_ID, rowId);
edit.putExtra(DBAdapter.Title, c.getString(c.getColumnIndex(DBAdapter.Title)));
edit.putExtra(DBAdapter.DATE, c.getString(c.getColumnIndex(DBAdapter.DATE)));
startActivity(edit);
}
});
这就是你要做的吗?