Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

java - What is the use of BaseColumns in Android

What is the use of implementing a class from BaseColumns in Android?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 of BaseColumns._ID) is required by CursorAdapter, implementations of a ContentProvider and other places where you hand off a Cursor to the Android platform to do things for you. For example, the adapter of a ListView uses the _ID column to give you the unique ID of the list item clicked in OnItemClickListener.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...