Android中BaseColumns有什么用

发布于 2024-12-11 16:50:17 字数 54 浏览 0 评论 0原文

在 Android 中实现 BaseColumns 中的类有什么用?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶情人眼里出诗心の 2024-12-18 16:50:17

BaseColumns 接口提供了各个列的名称常见的 _ID_COUNT 列。

使用通用名称使 Android 平台(以及开发人员)能够以统一的方式寻址任何数据项,无论其整体结构(即其他非 ID 列)如何。在接口/类中为常用字符串定义常量可以避免代码中的重复和拼写错误。

CursorAdapterContentProvider 以及其他将 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 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.

挽清梦 2024-12-18 16:50:17

这是一个简单的界面,添加了两个字段:

public interface BaseColumns
{
    /**
     * The unique ID for a row.
     * <P>Type: INTEGER (long)</P>
     */
    public static final String _ID = "_id";

    /**
     * The count of rows in a directory.
     * <P>Type: INTEGER</P>
     */
    public static final String _COUNT = "_count";
}

Android 中使用的内部 sqlite 数据库,带有一个自动递增并可用作主键的 _id 列。这也与 ContentProviders 很好地映射

This is a simple interface which adds two fields :

public interface BaseColumns
{
    /**
     * The unique ID for a row.
     * <P>Type: INTEGER (long)</P>
     */
    public static final String _ID = "_id";

    /**
     * The count of rows in a directory.
     * <P>Type: INTEGER</P>
     */
    public static final String _COUNT = "_count";
}

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 the ContentProviders

绝對不後悔。 2024-12-18 16:50:17

BaseColumn 接口仅提供列names _ID 和_COUNT。您仍然必须在构建表时指定使用它们的列。例如,要使用列名 _ID 创建列,您可以执行以下操作:

public static final String CREATE_TABLE =
    "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
    + _ID + " INTEGER PRIMARY KEY, "
    + USERNAME + " TEXT NOT NULL, "
    + PASSWORD + " TEXT NOT NULL, "
    + EMAIL + " TEXT NOT NULL UNIQUE)";

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:

public static final String CREATE_TABLE =
    "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
    + _ID + " INTEGER PRIMARY KEY, "
    + USERNAME + " TEXT NOT NULL, "
    + PASSWORD + " TEXT NOT NULL, "
    + EMAIL + " TEXT NOT NULL UNIQUE)";
慈悲佛祖 2024-12-18 16:50:17

它是一个接口,如下所示

public interface BaseColumns
{

    public static final String _ID = "_id";


    public static final String _COUNT = "_count";
}

它包含用于 SQL lite DB 中自动增量的 id 和 count 等常量。

我们还可以在不使用这个特定接口的情况下为 id 创建自己的常量。但是像光标适配器这样的函数需要像“_id”这样的精确常量,所以最好使用提供的接口!

希望这可以帮助你! :-)

It is an interface, which looks like this

public interface BaseColumns
{

    public static final String _ID = "_id";


    public static final String _COUNT = "_count";
}

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!! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文