How to get data in OnItemClickListener from SimpleCursorAdapter

*爱你&永不变心* 提交于 2019-12-10 14:17:26

问题


I'm trying to set onItemClickListener for my adapter, and it works but now i don't know how to get clicked object ? I have a list with notes and on click I want to start new activity with id of clicked note.

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);

     }
...

What should I insert into onItemClick to get note id(Toast is only for check if it works) ? I was looking for answer but I didn't find ;/

Thanks in advance

Greg


回答1:


well last element argument is the id of the clicked row. you can use that row to get data

    @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);
}


来源:https://stackoverflow.com/questions/5604700/how-to-get-data-in-onitemclicklistener-from-simplecursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!