Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data in OnItemClickListener from SimpleCursorAdapter

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

like image 480
Greg Avatar asked Nov 06 '22 00:11

Greg


1 Answers

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);
}
like image 54
Sunil Pandey Avatar answered Nov 14 '22 21:11

Sunil Pandey