ListView的行ID和位置索引混乱 [英] ListView row id and position index confusion
问题描述
我刚开始潜入一些基本的Android开发并已与实验一的ListView
,并用 SimpleCursorAdapter $它整合C $ C>。我经历了很多在线code样品的样子,但我也有一本书作为参考(专业的Android 2应用开发)。使用
I'm just starting to dive into some basic Android development and have been experimenting with a ListView
and integrating it with a SimpleCursorAdapter
. I look through a lot of online code samples, but I also have a book to use as a reference (Professional Android 2 Application Development).
在他们工作了一个例子待办事项存储在自动递增,整数,主键字段SQLite数据库列表项列表应用程序的书。
In the book they work out an example To-Do list application that stores the list items in a SQLite database with an auto-incrementing, integer, primary key field.
一个用户可以创建新的列表项,而且还可以从列表中删除选定的项目。在code,当删除时,主键字段限制由位置(SQL语句的
,而不是该项目的属性项目的 WHERE
子句中) ROWID
。
A user can create new list items, but can also delete a selected item from the list. In the code, when the delete occurs, the primary key field is restricted (within the WHERE
clause of the SQL statement) by the position
attribute of the item as opposed to the item's rowid
.
对我来说,这似乎是一个不正确的执行。望着SQLite的文档 AUTOINCREMENT
,它说,该值将随时增加和旧值将永远不会在同一个表重新使用。所以,如果你删除和添加的东西到列表中,这似乎是位置和行ID可以不同步的相当快。
To me, this seems like an incorrect implementation. Looking at the SQLite documentation for AUTOINCREMENT
, it says that this value will always increase and old values will never be re-used on the same table. So if you're deleting and adding things to the list, it would seem that the position and row id can get out of sync rather quickly.
我是正确的,那么,假设该行id是正确的方式指数到数据库表和不可以列表中的位置?我觉得位置是安全的使用,如果一个是使用常规 ListAdapter
,但到索引数据库时似乎并不合适。
Am I correct, then, to assume that the row id is the correct way to "index" into the database table and not the list position? I think the position would be safe to use if one is using the regular ListAdapter
, but doesn't seem suitable when indexing into the database.
推荐答案
这肯定是不好的做法。我总是用行ID删除,并且使用位置ID检索游标的行ID。我有这本书在国内的第一个版本,我打算以后看一看它自己。
That is definitely bad practice. I always use the row id to delete, and use the position id to retrieve the cursor's row id. I have the first edition of that book at home, I'm going to take a look at it myself later.
这篇关于ListView的行ID和位置索引混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!