Get selected Item Item Item (data filled by SQLite DB)

I have a database that tracks user reminder data (reminder name, notes, date, time, etc.). The first column is the primary key (_ID). The ListView is populated from the database and shows a simple set of rows with reminder names, for example:


Take out the trash.


Walk the dog.


Eat lunch.


and etc.


Now my question is: how do I get my application to find out which specific reminder was clicked? When I click a row, I need to find my primary key (column _ID) from my database and be able to retrieve all the data in that row.

So far, I know that I need to use onItemClick to detect a click. But how can I get the primary key value (_ID) of the item that was pressed? My current code is as follows:

final Context context = this; //DB Connectivity variables. protected RemindersDAO remindersDAO; protected SimpleCursorAdapter remindersCursorAdapter; public ListView viewRemindersListView; @SuppressWarnings("deprecation") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_local_reminders); // Get rid of the app title in the action bar. ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowTitleEnabled(false); viewRemindersListView = (ListView) findViewById(R.id.listview_view_local_reminders); remindersDAO = new RemindersDAO(this); Cursor cursor = remindersDAO.all(this); remindersCursorAdapter = new SimpleCursorAdapter(this, R.xml.view_reminders_item_layout, cursor, new String [] { RemindersDAO.NAME }, new int[] { R.id.view_reminders_item_text } ); viewRemindersListView.setAdapter(remindersCursorAdapter); } @Override public void onItemClick(AdapterView<?> listView, View view, int position, long arg3) { remindersDAO = new RemindersDAO(this); Cursor cursor = remindersDAO.all(this); int idColIndex = cursor.getColumnIndex(RemindersDAO._ID); int rowId = cursor.getInt(idColIndex); } @Override public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.activity_view_local_reminders, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { //Set the classes that are called from each actionbar item. switch (item.getItemId()) { case R.id.local_reminders_actionbar_go_advanced: Intent i=new Intent(this, AdvancedNewReminder.class); startActivity(i); return true; case R.id.local_reminders_actionbar_simple_reminder: Intent k = new Intent(this, QuickNewReminder.class); startActivity(k); return true; /* case R.id.local_reminders_actionbar_google_tasks: showGoogleTasksBetaDialog(); return true; */ } return false; } @Override public void onBackPressed() { return; } 

}

Thank you for your help!

+4
source share
1 answer

In the onItemClick method onItemClick you can see that the third parameter - long id - is the id in the database. So you just get this clicked item id

  @Override public void onItemClick(AdapterView<?> listView, View view, int position, long arg3) { Log.d("Clicked item id", " "+ arg3); } 

if you need other fields, you should get the cursor of the clicked element

  @Override public void onItemClick(AdapterView<?> listView, View view, int position, long arg3) { Cursor item= (Cursor) reminderCursorAdapter.getItem(position); Log.d("Clicked item field", " "+ item.getColum(your colum index)); } 
+5
source

Source: https://habr.com/ru/post/1434967/


All Articles