使用本地 SQlite 数据库填充可扩展列表视图的方法

发布于 2024-11-25 03:00:30 字数 421 浏览 1 评论 0原文

我的应用程序中有一个 sqlite 数据库。我想用它制作一个可扩展的列表视图。

我已经确定了我应该采取的方法。

尝试了很多方法来找到相同的教程,但找不到一个使用本地数据库填充可扩展列表的教程。

Android 网站上有一个教程,他们使用手机中的联系人详细信息填充可扩展列表。他们通过内容提供商执行此操作 ExpandableList2.java

所以我的问题是应该我还为我自己的应用程序内部的数据库创建了一个内容提供程序?

这是唯一的方法还是还有其他更好的方法?

I have a sqlite database in my application. I want to make an expandable list view with that.

I am fixed with the approach I should take for that.

Tried a lot to find a tutorial for the same, but could not find a single one, where one is populating the Expandable list with the local database.

There is this one tutorial in the android site where they are filling the expandable list with the Contact detail in the phone. They are doing this from the content provider
ExpandableList2.java

So my question is should I also create a content provider for my database which is inside my own application ?

Is this the only approach or is there any other better one?

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

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

发布评论

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

评论(3

猛虎独行 2024-12-02 03:00:30

我创建了一个项目来尝试可扩展列表视图和数据库,希望这有帮助

 final class ExpAdapter extends CursorTreeAdapter {
        LayoutInflater mInflator;

        public ExpAdapter(Cursor cursor, Context context) {
            super(cursor, context);
            mInflator = LayoutInflater.from(context);
        }

        @Override
        protected void bindChildView(View view, Context context, Cursor cursor,
                boolean isLastChild) {
            TextView tvChild = (TextView) view.findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
        }

        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor,
                boolean isExpanded) {
            TextView tvGrp = (TextView) view.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            int groupId = groupCursor.getInt(groupCursor
                    .getColumnIndex(DBHelper.COL_ID));
            return aDBHelper.getChildCursor(groupId);
        }

        @Override
        protected View newChildView(Context context, Cursor cursor,
                boolean isLastChild, ViewGroup parent) {
            View mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
            TextView tvChild = (TextView) mView
                    .findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
            return mView;
        }

        @Override
        protected View newGroupView(Context context, Cursor cursor,
                boolean isExpanded, ViewGroup parent) {
            View mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
            TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
            return mView;
        }

    }

I had created a project to try out expandablelistview and database, hope this helps

 final class ExpAdapter extends CursorTreeAdapter {
        LayoutInflater mInflator;

        public ExpAdapter(Cursor cursor, Context context) {
            super(cursor, context);
            mInflator = LayoutInflater.from(context);
        }

        @Override
        protected void bindChildView(View view, Context context, Cursor cursor,
                boolean isLastChild) {
            TextView tvChild = (TextView) view.findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
        }

        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor,
                boolean isExpanded) {
            TextView tvGrp = (TextView) view.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            int groupId = groupCursor.getInt(groupCursor
                    .getColumnIndex(DBHelper.COL_ID));
            return aDBHelper.getChildCursor(groupId);
        }

        @Override
        protected View newChildView(Context context, Cursor cursor,
                boolean isLastChild, ViewGroup parent) {
            View mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
            TextView tvChild = (TextView) mView
                    .findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
            return mView;
        }

        @Override
        protected View newGroupView(Context context, Cursor cursor,
                boolean isExpanded, ViewGroup parent) {
            View mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1, null);
            TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(cursor
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
            return mView;
        }

    }
原谅我要高飞 2024-12-02 03:00:30

您不必为此创建内容提供商。

  1. 创建一个 DBHelper 类并创建函数,用于在没有内容提供程序的情况下从本地数据库获取记录。
  2. 创建一个模型类来保存本地数据库中的记录。
  3. 使用此模型类作为列表适配器的数据。

You don't have to create a content provider for that.

  1. Create a DBHelper class and create functions for fetching records from your local database without a content provider.
  2. Create a model class for holding the records from your local database.
  3. Use this model class as your data for your list adapter.
同尘 2024-12-02 03:00:30

如果您有本地数据库,最好的方法是使用 CursorTreeAdapter,因为它会自动处理列表更新。

查看这篇文章我会帮助你。
ExpandableListView 使用 CursorTreeAdapter 与片段、内容提供程序、LoaderCallbacks 和 CursorLoader

If your having local database, best approach is to go with CursorTreeAdapter because it automatically handles updating the list.

Check this post my help you.
ExpandableListView using CursorTreeAdapter with in fragment, Content Provider, LoaderCallbacks and CursorLoader

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