Edit List Items in ListView

I have a list from SQLDatabase using a custom CursorAdaptor. I would like to go back to the activity that I used to create items, when I click on them in listview.so that I can edit posts. But nothing happens when I implement the OnItemClick and getItemId () methods in the CursorAdapter, although I'm not sure if I'm right. Here is my code:

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; } 
+4
source share
1 answer

I think you need to set onItemClickListener in the Listview, and not try to implement it in the Adapter class.

Example:

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

Is that what you do?

+3
source

All Articles