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

时间:2021-08-24 10:23:03

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

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

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.

尝试了很多相同的教程,却找不到一个教程,其中一个用本地数据库填充Expandable列表。

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

在android站点中有一个教程,他们用手机中的联系人详细信息填充可扩展列表。他们是从内容提供商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?

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

3 个解决方案

#1


3  

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

我创建了一个项目来试用expandablelistview和数据库,希望这会有所帮助

 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;
        }

    }

#2


2  

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. 创建DBHelper类并创建用于从本地数据库获取记录而无需内容提供程序的函数。
  3. Create a model class for holding the records from your local database.
  4. 创建一个模型类,用于保存本地数据库中的记录。
  5. Use this model class as your data for your list adapter.
  6. 使用此模型类作为列表适配器的数据。

#3


0  

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

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

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

查看这篇文章我的帮助。在片段,Content Provider,LoaderCallbacks和CursorLoader中使用CursorTreeAdapter的ExpandableListView

#1


3  

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

我创建了一个项目来试用expandablelistview和数据库,希望这会有所帮助

 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;
        }

    }

#2


2  

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. 创建DBHelper类并创建用于从本地数据库获取记录而无需内容提供程序的函数。
  3. Create a model class for holding the records from your local database.
  4. 创建一个模型类,用于保存本地数据库中的记录。
  5. Use this model class as your data for your list adapter.
  6. 使用此模型类作为列表适配器的数据。

#3


0  

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

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

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

查看这篇文章我的帮助。在片段,Content Provider,LoaderCallbacks和CursorLoader中使用CursorTreeAdapter的ExpandableListView