如何从SimpleCursorAdapter获取OnItemClickListener中的数据

时间:2011-04-09 11:20:24

标签: android simplecursoradapter

我正在尝试为我的适配器设置onItemClickListener,它可以工作,但现在我不知道如何获得点击的对象?我有一个带注释的列表,点击后我想开始点击备注id的新活动。

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中插入什么来获取注释ID(Toast仅用于检查它是否有效)?我正在寻找答案,但我没有找到; /

提前致谢

格雷格

1 个答案:

答案 0 :(得分:2)

最后一个元素参数是单击行的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);
}