【问题标题】:Populate ExpandableListView with data from SQLite使用 SQLite 中的数据填充 ExpandableListView
【发布时间】:2016-08-07 14:59:13
【问题描述】:

设置:我有一个本地 SQLite 数据库,用于保存电影的放映时间。其中一些列是“日期”、“ID”、“时间”...

意图: 我想将所有这些放映时间放入 ExpandableListView,而日期将成为父项。

问题:什么都没有显示。什么都没有。我很难理解为什么。有人有想法吗?

一些代码:最重要的东西在顶部,向下滚动时变得越来越不重要......

这是我试图实现此目的的代码,在 Activity 的 onCreate() 中调用:

String sql = "SELECT rowid _id,* FROM " + Statics.DBSHOWS + ";";
Cursor movieCursor = database.rawQuery(sql,null);
movieCursor.moveToFirst();
adapter = new ExpandableListAdapter(movieCursor, this,
            android.R.layout.simple_list_item_1,
            android.R.layout.simple_list_item_2,
            new String[]{Statics.DBSHOWS_DATE},
            new int[]{android.R.id.text1},
            new String[]{Statics.DBSHOWS_TIME, Statics.DBSHOWS_ID},
            new int[]{android.R.id.text1, android.R.id.text2});

movieListView.setAdapter(adapter);

这是我的 ExpandableListAdapter:

public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
    Context context;

    public ExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
                                 int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                                 int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
        this.context = context;
    }



    @Override
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
        super.bindChildView(view, context, cursor, isLastChild);
        System.out.println("Dumping form Childview");
        DatabaseUtils.dumpCurrentRow(cursor);
    }

    @Override
    protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
        super.bindGroupView(view, context, cursor, isExpanded);
        if(view==null){
            System.out.println("View is null!!");
        }
        System.out.println("Dumping form Groupview");
        DatabaseUtils.dumpCurrentRow(cursor);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        int dateInMillis = groupCursor.getInt(groupCursor.getColumnIndex(Statics.DBSHOWS_DATE));
        Cursor childCursor = DataHandler.getShowsForDate(dateInMillis);
        childCursor.moveToFirst();
        return childCursor;
    }

}

两个Override 方法bindChildView(...)bindGroupView(...) 用于测试。正如预期的那样,打印了以下输出:

Dumping form Groupview
0 {
    _id=1
    Id=117451
    Date=15.04.2016
    Time=20:15
    Movie_Id=2181
    Hall=0
    Cards_Sold=0
}

【问题讨论】:

  • 转储movieCursor (DatabaseUtils#dumpCursor)
  • 打印一切都很好......
  • 覆盖测试bindGroupView / bindChildView 看看有没有被调用,你可以在里面调用DatabaseUtils#dumpCurrentRow
  • 导致打印 11 个转储,所有转储都包含完整和逻辑数据...:/
  • 所以如果视图被绑定,唯一的原因可能是“白色背景上的白色文本”综合症......

标签: android sqlite expandablelistview


【解决方案1】:

我对我们的爱好/工作有着又爱又恨的关系:你的某些东西在某些时候起作用,某些东西似乎完全独立地发生了变化,然后你的某些东西又不再起作用了。这就是这里发生的事情,你不可能猜到它:

随着我的设备更新到 Marshmallow,它仍然可以读取我之前使用的 ArrayList 但无法再读取数据库...为什么它仍然可以转储光标...?我不知道。但是,一旦我发现您必须在旅途中请求权限并实施它,它就可以工作了......无论如何。

【讨论】:

  • 我喜欢我们的工作:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多