9

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

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

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

在 android 站点中有一个教程,他们在其中使用手机中的联系人详细信息填充可扩展列表。他们是从内容提供者 ExpandableList2.java做的

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

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

4

3 回答 3

3

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

最终类 ExpAdapter 扩展 CursorTreeAdapter {
        LayoutInflater mInflator;

        public ExpAdapter(Cursor cursor, Context context) {
            超级(光标,上下文);
            mInflator = LayoutInflater.from(context);
        }

        @覆盖
        protected void bindChildView(View view, Context context, Cursor cursor,
                布尔 isLastChild) {
            TextView tvChild = (TextView) view.findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(光标
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
        }

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

        @覆盖
        受保护的光标 getChildrenCursor(光标组光标) {
            int groupId = groupCursor.getInt(groupCursor
                    .getColumnIndex(DBHelper.COL_ID));
            返回 aDBHelper.getChildCursor(groupId);
        }

        @覆盖
        protected View newChildView(上下文上下文,光标光标,
                boolean isLastChild, ViewGroup parent) {
            查看 mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1,空);
            TextView tvChild = (TextView) mView
                    .findViewById(android.R.id.text1);
            tvChild.setText(cursor.getString(光标
                    .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME)));
            返回 mView;
        }

        @覆盖
        protected View newGroupView(上下文上下文,光标光标,
                boolean isExpanded, ViewGroup parent) {
            查看 mView = mInflator.inflate(
                    android.R.layout.simple_expandable_list_item_1,空);
            TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
            tvGrp.setText(cursor.getString(光标
                    .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME)));
            返回 mView;
        }

    }
于 2011-07-21T06:58:39.450 回答
2

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

  1. 创建一个 DBHelper 类并创建用于在没有内容提供者的情况下从本地数据库中获取记录的函数。
  2. 创建一个模型类来保存本地数据库中的记录。
  3. 将此模型类用作列表适配器的数据。
于 2014-09-19T11:18:32.957 回答
0

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

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

于 2014-09-20T05:02:18.760 回答