如何获取来自SimpleCursorAdapter的OnItemClickListener中的数据
问题描述:
我想为我的适配器设置onItemClickListener,它可以工作,但现在我不知道如何获取点击对象?我有一个列表与笔记,并点击我想开始与点击笔记ID的新活动。如何获取来自SimpleCursorAdapter的OnItemClickListener中的数据
private DatabaseHelper dbhelper;
SimpleCursorAdapter adapter = null;
public OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "it works", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.first);
dbhelper = new DatabaseHelper(getApplicationContext());
Cursor cursor = dbhelper.getAllNotes();
startManagingCursor(cursor);
ListView lv = (ListView)findViewById(android.R.id.list);
String[] columns = new String[] {"NoteTitle", "NoteDate"};
int[] to = new int[] { R.id.title_entry, R.id.date_entry};
adapter = new SimpleCursorAdapter(this, R.layout.note_entry, cursor, columns, to);
lv.setAdapter(adapter);
lv.setOnItemClickListener(listener);
}
...
public Cursor getAllNotes()
{
SQLiteDatabase db=this.getReadableDatabase();
return db.query(noteTable, new String [] {colID, colTitle, colDesc, colDate}, null, null, null, null, null);
}
...
我应该在onItemClick中插入什么来获取note标识(Toast仅用于检查它是否工作)?提前/
感谢
格雷格
答
以及最后一个元素参数是点击行的id,我一直在寻找的答案,但我没有找到。您可以使用该行获取数据
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, MyCustomDetailActivity.class);
intent.putExtra("item-identifier", id);
startActivity(intent);
}
您能否告诉我如何在另一活动中接收意图?我想在另一个activity的edittext中设置listview项目的内容。我已经按照你的回答,但我不知道如何在另一个活动中获取所选项目的ID和数据库内容。 – Apurva 2015-02-01 10:12:03