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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…