android - 使用本地 SQlite 数据库填充可扩展 ListView 的方法

标签 android sqlite expandablelistview android-contentprovider simplecursoradapter

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

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

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

android 站点中有一个教程,他们在其中用手机中的联系方式详细信息填充可扩展列表。他们从内容提供商那里这样做 ExpandableList2.java

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

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

最佳答案

我已经创建了一个项目来试用 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;
        }

    }

关于android - 使用本地 SQlite 数据库填充可扩展 ListView 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6760948/

相关文章:

android - 如何使用 FieldValue.serverTimestamp() 在 android 中自定义模型类

android - 当应用程序崩溃并且 logcat 没有显示错误时该怎么办

android - 如何实现模拟位置?

android - ORMLite无法在Android中使用唯一的组合创建行

android - 我们如何知道我们的应用程序数据何时在 android 中被清除?

Android - 如何在 ListView 或 ExpandableListView 中为 TextView 设置填充

android - 如何在 NavigationView android 中添加 N 级可扩展菜单?

android - 我应该在 Activity 中缓存来自 SharedPreferences 的数据吗?

python-2.7 - 如何实际更改SQLITE中的最大列数

android - 可扩展的 ListView 组指示器在获得焦点时自动更改