Why/Should we implement BaseColumns when using a Content Provider in Android? -
i browsing source code google iosched app , noticed following code snippet part of content provider implementation:
public static class blocks implements blockscolumns, basecolumns
.
as far know basecolumns interface 2 constants: _count
, _id
.
i have 2 questions:
what advantages/disadvantages of implementing basecolumns opposed having private field
_id
in class directly?what role of constant
_count
?
according android developer guide,
note: provider isn't required have primary key, , isn't required use _id column name of primary key if 1 present. however, if want bind data provider listview, 1 of column names has _id. requirement explained in more detail in section displaying query results.
the guide continues on explain basics of why need unique value provided primary key
,
table data should have "primary key" column provider maintains unique numeric value each row. can use value link row related rows in other tables (using "foreign key"). although can use name column, using basecolumns._id best choice, because linking results of provider query listview requires 1 of retrieved columns have name _id. [emphasis mine]
to answer questions in order provided them:
- having
_id
column best practice versatility. doesn't have displayed, works terrific primary key (and foreign key!) required cursors , queries.- having identified basecolumns automatically identifies column primary key, unique (obviously), , instructs autoincrement.
- presumably, implementing basecolumns easier typing out these properties private fields.
_count
is count of number of rows in directory. if table's rows being deleted , added, there no reason believe item's_id
integer has when added or sort properties. in other words,last_insert_rowid()
does not equalcount()
. _count column provides way show how many results returned on query, in every line of query. visual reference, see linuxtopia.org
Comments
Post a Comment