1

I completed the Notepad Tutorial Part 2 and everythings works fine. But there is one thing I just can´t figure out why it is working ;)

The onListItemClick callback retreives the parameters:

  • ListView l - Check
  • View v - Check
  • position - Position of the item the user clicked on, starting with a zero based index (right?)
  • id - Row ID of the item the user clikced on

This seems to be the same rowId like used in the SqlLite DB table "notes"...but where the hell does the ListActivity know about that we use this column as row Id? I didn´t find any mapping between the List and the DB table, just the Cursor we bound in fillData. But there is only a mapping between the Title column and the text1 id in the UI. So where is the rowId binding? And what if I wanted to change that binding to another source?

Thx in advance Alex

2 Answers2

0

This is NOT a rowID like in SQL. Databases are completely different from form elements.

rowID in this context is simply a 0-based index to tell you what you are clicking on. Your Cursor is simply filling data into the ListView from a table. Unless you are making direct queries with ContentProviders or SQLite queries, you shouldn't have to select the rows.

I hope this helps!

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • Thanks for your answer but this raises up 2 more qquestions: If this is not the rowID of the DB, why can we just put the 'id' parameter from the onListItemClick method to the intent like editIntent.putExtra(NotesDbAdapter.KEY_ROWID, id)? That would infer that the DB.rowID=ListView.Id. The other question is the difference between the position and id paramter? In my example I had 2 entries and I clicked the 2nd having position=1 and rowId=4 assuming that I added and deleted notes before debugging. –  Nov 08 '11 at 16:13
  • @Alexander Beer see my comment ... anyway ... it can be ROWID from db or any other PK from your table ... all you need to do is "rename" it in query like "SELECT blabla AS _id, rest, columns, go, here FROM table" ... EDIT: forgot to mention ... AFAIK in sqlite if you use your own integer/long PK, ROWID points to this column – Selvin Nov 08 '11 at 16:22
  • Ah thanks, this would make sense! It´s a pity the tutorial doesn´t mention it at all. –  Nov 08 '11 at 16:28
0

The SimpleCursorAdapter (in the fillData() method) binds the data of the database cursor to the listview. Whenever an item is clicked the CursorAdapter realizes which item is clicked and handes over the id to the listactivity.

Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
  • Thanks for your answer. But what is with the deleteNote() operation in NotesDBAdapter? It receives the rowId and executes a delete operation like return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null). The dleteNote() method is called in onContextItemSelected() with AdapterContextMenuInfo.id. Doesnßt this also imply that the rowId in the UI must be the same in the DB? Or is this just by "random"? Sorry Im confused ;) –  Nov 08 '11 at 16:25