What is the utility of using Basecolumns. _ID to define a contract class in Sqlite?

Asked

Viewed 59 times

0

Why should I wear Basecolumns. _ID instead of simply defining a string as _id ?

For example, instead of using:

  • public static final String _ID = BaseColumns._ID;

Wouldn’t it be simpler to use this?

  • public static final String _ID = "_id";

1 answer

1


In the structure of your tables, there is no utility at all and you can simply define public static final String _ID = "_id";

But why is there this constant in Android?

This constant serves for you to have the correct value of the field ID when filtering something from the Android.

Example of how to filter call logs.

Cursor c = contentResolver.query(CallLog.Calls.CONTENT_URI,
        new String[] { CallLog.Calls.DATE, CallLog.Calls.DURATION,
                CallLog.Calls.NUMBER, BaseColumns._ID },
        BaseColumns._ID + " = ?",
        new String[] { "1" },
        CallLog.Calls.NUMBER + " asc");

Note that in the code above we use BaseColumns._ID. In such cases it is necessary to use this way, because the Android can change this field value ID in the future and with that our code is kept up to date.

Already if we used _id (as in the example below) our code would no longer work.

Cursor c = contentResolver.query(CallLog.Calls.CONTENT_URI,
        new String[] { CallLog.Calls.DATE, CallLog.Calls.DURATION,
                CallLog.Calls.NUMBER, "_id" },
        "_id = ?",
        new String[] { "1" },
        CallLog.Calls.NUMBER + " asc");

So when is its structure When using the data structure of the Android, use the available constants.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.