Android中BaseColumns有什么用
在 Android 中实现 BaseColumns
中的类有什么用?
What is the use of implementing a class from BaseColumns
in Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 Android 中实现 BaseColumns
中的类有什么用?
What is the use of implementing a class from BaseColumns
in Android?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
BaseColumns
接口提供了各个列的名称常见的_ID
和_COUNT
列。使用通用名称使 Android 平台(以及开发人员)能够以统一的方式寻址任何数据项,无论其整体结构(即其他非 ID 列)如何。在接口/类中为常用字符串定义常量可以避免代码中的重复和拼写错误。
CursorAdapter
,ContentProvider
以及其他将Cursor
交给 Android 平台来为您做事的地方。例如,ListView
的适配器使用_ID
列为您提供在OnItemClickListener.onItemClick()
,而无需每次都明确指定 ID 列是什么。是否实现仅由常量组成的接口或引用它们的全名(即
BaseColumns._ID
)取决于个人喜好。我个人更喜欢后者,因为_ID
的来源更加明显,而前者感觉像是滥用继承。The
BaseColumns
interface provides names for the very common_ID
and_COUNT
columns.Using common names enables the Android platform (and developers as well) to address any data item, regardless of its overall structure (i.e. other, non-ID columns) in a unified way. Defining constants for commonly used strings in an interface/class avoids repetition and typos all over the code.
Using a column named
_id
(the constant value ofBaseColumns._ID
) is required byCursorAdapter
, implementations of aContentProvider
and other places where you hand off aCursor
to the Android platform to do things for you. For example, the adapter of aListView
uses the_ID
column to give you the unique ID of the list item clicked inOnItemClickListener.onItemClick()
, without you having to explicitly specify what your ID column is every time.Whether or not to implement interfaces consisting only of constants or reference them with their full name, i.e.
BaseColumns._ID
is a matter of taste. I personally prefer the latter, because it's more obvious where_ID
is coming from and the former feels like an abuse of inheritance.这是一个简单的界面,添加了两个字段:
Android 中使用的内部 sqlite 数据库,带有一个自动递增并可用作主键的
_id
列。这也与 ContentProviders 很好地映射This is a simple interface which adds two fields :
Internally sqlite databases used in Android, comes with an
_id
column that autoincrements and can function as a primary key. This also maps well with theContentProviders
BaseColumn 接口仅提供列names _ID 和_COUNT。您仍然必须在构建表时指定使用它们的列。例如,要使用列名 _ID 创建列,您可以执行以下操作:
The BaseColumn interface only provides the column names _ID and _COUNT. You must still specify columns that use them when constructing tables. For example, to create a column using the column name _ID you might do the following:
它是一个接口,如下所示
它包含用于 SQL lite DB 中自动增量的 id 和 count 等常量。
我们还可以在不使用这个特定接口的情况下为 id 创建自己的常量。但是像光标适配器这样的函数需要像“_id”这样的精确常量,所以最好使用提供的接口!
希望这可以帮助你! :-)
It is an interface, which looks like this
It contains constants like id and count used for auto-increment in SQL lite DB.
We can also create our own constants for id without using this particular interface. But functions like cursor adaptor requires the exact constants like "_id", So it is better to use the provided interface!!
Hope this helps you out!! :-)