开发者

How does the delete row functionality work in the Android notepad tutorial?

开发者 https://www.devze.com 2023-03-16 18:46 出处:网络
I\'m currently working through the notepad tutorial, and exercise 2 completes the code to delete notes.However, I\'m slightly confused how this works.Here\'s the relevant code:

I'm currently working through the notepad tutorial, and exercise 2 completes the code to delete notes. However, I'm slightly confused how this works. Here's the relevant code:

public boolean onContextItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case DELETE_ID:
            AdapterContextMenuInfo info = 
                    (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteNote(info.id);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

The exercise states: "The id field of this [AdapterContextMenuInfo] object tells us the position of the item in the ListView. We then pass this to the deleteNote() method of our N开发者_Go百科otesDbAdapter and the note is deleted."

Looking at the database definition, the id fields of newly added rows/notes to the database are auto-generated by an auto-incrementing number. Therefore, if we have 4 notes with id's 1,2,3,4 (not sure if its zero indexed or not!) and delete the 2nd note, shouldn't we be left with ids 1,3,4? Which means that trying to delete the last note (which is now 3rd in the list, but still with its original 4th index) should instead delete the row with the index=3? Or do the rows get auto-reindexed when a row gets deleted?

Finally, can you peek/browse the database on your phone for an app?


It seems to me that someone made a small mistake when writing the example's text. If you see the documentation for the id field of AdapterContexteMenuInfo, it states that it returns the row id of the element. This is provided by your Adapter's getId() method.

And, yes, you can read your phone's database. You connect to the emulator shell (if you're using the emulator) using this command from the command line:

   adb -e shell

You then go into your application's folder (in /data/data/com.yourpackage). There should be a databases folder. From the shell, type sqlite3 <databaseFileName>. You can then do any supported database operations (select, update, etc). Check this page out, it even has a section on sqlite3.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号