ExpandableListView中的错误

时间:2012-10-20 16:11:51

标签: android expandablelistview expandablelistadapter

这是我的ExpandableList活动的代码

public class ExpenseEditorActivity extends ExpandableListActivity {
    MyDOA projedit = new MyDOA(ExpenseEditorActivity.this, Environment.getExternalStorageDirectory());
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lv_Expenses = getExpandableListView();
        projedit.open();
        fillExpenseList();
        lv_Expenses.setAdapter(exp_adapter);
    }


    public  void fillExpenseList(){
        Cursor groupCursor=projedit.fetchGroup(SelectProjectActivity.sel_id);
        startManagingCursor(groupCursor);
        groupCursor.moveToFirst();
        exp_adapter=new MyExpandableListAdapter(groupCursor,this, R.layout.expense_list_expandableitem, R.layout.expense_list_item,new String[]{"expdate"}, new int[]{R.id.label_date},new String[]{"expname","expamount"},new int[]{R.id.label_eh,R.id.label_amount});

    }

    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
        public MyExpandableListAdapter(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);
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            Cursor childCursor= projedit.fetchChildren(SelectProjectActivity.sel_id, groupCursor.getString(0));            
            startManagingCursor(childCursor);
            childCursor.moveToFirst();
            return childCursor;
        }
    }
}

当活动运行正常时,只要expandablelistview未展开(默认情况下),但是当我点击“group”进行展开时,我会收到以下logcat错误

FATAL EXCEPTION: main java.lang.IllegalStateException: get field slot from row 1 col -1 failed 
    at android.database.CursorWindow.getLong_native(Native Method) 
    at android.database.CursorWindow.getLong(CursorWindow.java:412) 
    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
    at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:437)
    at android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:192)
    at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:421)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1992)
    at android.os.Handler.handleCallback(Handler.java:587) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:130) 
    at android.app.ActivityThread.main(ActivityThread.java:3687) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
    at dalvik.system.NativeStart.main(Native Method)

有人可以阅读此内容并告诉我为什么当我点击任何“群组”时出现这些错误。

此外,如果我将所有组设置为在我的代码中展开。一旦活动开始(显然)我就会得到相同的错误

修改

public Cursor fetchChildren(int i,String date){
Cursor cursor=database.query("expense,expensehead", new String[]{"expense._id","expensehead.expname","expense.expamount","expensehead._id"},
"expense.project_id=\""+i+"\" and expensehead.project_id=\""+i+"\" and expense.exphead_id=expensehead._id and expense.expdate=\""+date+"\"", null, null, null, null);
return cursor;
}

public Cursor fetchGroup(int i){
Cursor cursor=database.query("expense", new String[]{"expdate"}, "project_id=\""+i+"\"",null, "expense.expdate", null, null);
return cursor;
}

我的数据库看起来像这样

database.execSQL("create table project(_id integer primary key autoincrement, "+
                "name text not null, " +
                "amount real not null, " +
                "startdate text not null, " +
                "enddate text not null, "+
                "avgbase integer not null);");
        database.execSQL("create table expensehead(_id integer primary key autoincrement, "+
                "expname text not null, "+
                "project_id integer not null);");
        database.execSQL("create table expense(_id integer primary key autoincrement, "+
                "expamount real not null, "+
                "expdate text not null, "+
                "exphead_id integer not null, "+
                "project_id integer not null);");

1 个答案:

答案 0 :(得分:0)

来自CursorTreeAdapter's documentation

  

将数据从一系列游标公开到一个游标的适配器   ExpandableListView小部件。顶级Cursor(在。中给出)   构造函数)暴露组,而后续的游标返回   来自getChildrenCursor(Cursor)暴露特定的孩子   组。游标必须包含名为“_id”的列或此类   不行。

如果我错了,请纠正我,但要确保您的groupCursor也有_id,您需要将您的小组字段放在单独的表格中。

所以我用_id p.k制作了一张日期表。并且该列表现在正常工作。