Android 记事本教程中的删除行功能如何工作?
我目前正在学习记事本教程,以及练习 2完成删除注释的代码。但是,我有点困惑这是如何工作的。下面是相关代码:
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);
}
练习指出:“这个 [AdapterContextMenuInfo] 对象的 id 字段告诉我们该项目在 ListView 中的位置。然后我们将其传递给 NotesDbAdapter 的 deleteNote() 方法,然后便笺被删除。”
查看数据库定义,新添加到数据库的行/注释的 id 字段是通过自动递增的数字自动生成的。因此,如果我们有 4 个 id 为 1,2,3,4 的笔记(不确定它的索引是否为零!)并删除第二个笔记,难道我们不应该留下 id 1,3,4 吗?这意味着尝试删除最后一个注释(现在是列表中的第三个注释,但仍具有其原始的第四个索引)应该删除索引 = 3 的行?或者当行被删除时,行是否会自动重新索引?
最后,您可以查看/浏览手机上的数据库以查找应用程序吗?
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 NotesDbAdapter 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,有人在编写示例文本时犯了一个小错误。如果您看到 AdapterContexteMenuInfo,它声明它返回元素的行 ID。这是由
Adapter
的getId()
方法提供的。是的,您可以读取手机的数据库。您可以从命令行使用以下命令连接到模拟器 shell(如果您正在使用模拟器):
然后进入应用程序的文件夹(在 /data/data/com.yourpackage 中)。应该有一个
databases
文件夹。在 shell 中,输入sqlite3
。然后您可以执行任何支持的数据库操作(选择、更新等)。查看此页面,它甚至有关于 sqlite3 的部分。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 yourAdapter
'sgetId()
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:
You then go into your application's folder (in /data/data/com.yourpackage). There should be a
databases
folder. From the shell, typesqlite3 <databaseFileName>
. You can then do any supported database operations (select, update, etc). Check this page out, it even has a section on sqlite3.