Android ExpandableListView sqlite错误
我有一个包含事件名称和日期的数据库。我正在尝试使用游标创建一个expandablelistview,该游标将按事件日期对事件进行分组 我没有找到很多与我的情况相关的信息。SDK中的例子是使用内容提供者,我还没有读过/尝试过,我已经用旧的方法设置了数据库助手 我发现我认为是一个很好的例子,并试图将它适应我的使用,但它崩溃了,我无法找出我做错了什么 我的代码Android ExpandableListView sqlite错误,android,android-widget,Android,Android Widget,我有一个包含事件名称和日期的数据库。我正在尝试使用游标创建一个expandablelistview,该游标将按事件日期对事件进行分组 我没有找到很多与我的情况相关的信息。SDK中的例子是使用内容提供者,我还没有读过/尝试过,我已经用旧的方法设置了数据库助手 我发现我认为是一个很好的例子,并试图将它适应我的使用,但它崩溃了,我无法找出我做错了什么 我的代码 import android.app.ExpandableListActivity; import android.content.Conte
import android.app.ExpandableListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorTreeAdapter;
public class ExpandableListTestActivity extends ExpandableListActivity {
private AttendanceDB mDbHelper;
private Cursor mGroupsCursor;
private int mGroupIdColumnIndex;
private MyExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new AttendanceDB(this);
mDbHelper.open();
fillData();
}
private void fillData() {
mGroupsCursor = mDbHelper.fetchGroup();
startManagingCursor(mGroupsCursor);
mAdapter = new MyExpandableListAdapter(mGroupsCursor,this,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_1,
new String[] { AttendanceDB.EVENT_DATE },
new int[] { android.R.id.text1 },
new String[] { AttendanceDB.EVENT_NAME },
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
}
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 = mDbHelper
.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex(AttendanceDB.EVENT_DATE)));
startManagingCursor(childCursor);
return childCursor;
}
}
}
当我运行此代码时,我得到以下信息:
E/AndroidRuntime(30329): FATAL EXCEPTION: main
E/AndroidRuntime(30329): java.lang.RuntimeException: Unable to start activity ComponentInfo{cdc.workshopapps.explist/cdc.workshopapps.explist.ExpandableListTestActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it
E/AndroidRuntime(30329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
E/AndroidRuntime(30329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
E/AndroidRuntime(30329): at android.app.ActivityThread.access$500(ActivityThread.java:122)
E/AndroidRuntime(30329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
E/AndroidRuntime(30329): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(30329): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(30329): at android.app.ActivityThread.main(ActivityThread.java:4123)
E/AndroidRuntime(30329): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(30329): at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(30329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(30329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(30329): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(30329): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it
E/AndroidRuntime(30329): at android.database.CursorWindow.getLong_native(Native Method)
E/AndroidRuntime(30329): at android.database.CursorWindow.getLong(CursorWindow.java:436)
E/AndroidRuntime(30329): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:56)
E/AndroidRuntime(30329): at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:437)
E/AndroidRuntime(30329): at android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:192)
E/AndroidRuntime(30329): at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:421)
E/AndroidRuntime(30329): at android.widget.AdapterView.getItemIdAtPosition(AdapterView.java:744)
E/AndroidRuntime(30329): at android.widget.AdapterView.setSelectedPositionInt(AdapterView.java:1087)
E/AndroidRuntime(30329): at android.widget.ListView.setAdapter(ListView.java:458)
E/AndroidRuntime(30329): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:469)
E/AndroidRuntime(30329): at android.app.ExpandableListActivity.setListAdapter(ExpandableListActivity.java:246)
E/AndroidRuntime(30329): at cdc.workshopapps.explist.ExpandableListTestActivity.fillData(ExpandableListTestActivity.java:36)
E/AndroidRuntime(30329): at cdc.workshopapps.explist.ExpandableListTestActivity.onCreate(ExpandableListTestActivity.java:22)
E/AndroidRuntime(30329): at android.app.Activity.performCreate(Activity.java:4397)
E/AndroidRuntime(30329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
E/AndroidRuntime(30329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
E/AndroidRuntime(30329): ... 11 more
我认为这意味着我在游标中返回的列有问题。但是如果我将filldata()方法更改为以下内容(只是为了检查我的游标/列):
祝酒会输出预期的事件日期和事件名称数据,因此我不知道哪里出错了
其他(可能)相关代码:
public Cursor fetchGroup() {
String query ="SELECT DISTINCT date FROM events";
return mDb.rawQuery(query,null);
}
public Cursor fetchChildren(String date) {
Cursor c = mDb.query(EVENT_TABLE, new String[] { EVENT_ROWID,
EVENT_NAME }, EVENT_DATE + "='" + date + "'" ,
null, null, null, null);
return c;
}
任何帮助都将不胜感激。在从游标获取数据之前,您必须调用Cursor.moveToFirst();否则它不会初始化。您在getChildrenCursor()函数中遗漏了这一点 加:
CursorTreeAdapter在光标中需要一个“_id”字段。如果数据库中有此字段,只需将其添加到fetchGroup和fetchChildren查询中即可 否则,添加“rowid as_id”。这使用SQLite内部行编号
不幸的是,这不是问题所在(尽管我添加了一个moveToFirst(),以防万一,但还是出现了相同的错误)。问题似乎是我的光标上缺少了一列,但我肯定也不知道在哪里/如何找到。我会在一堆圣经上发誓,我读了三遍那个链接,却错过了关于需要id的那一点。它正在工作。现在我只需要让它在一个片段中工作p但如果我遇到问题,这可能是另一天的问题。谢谢你的帮助!
public Cursor fetchGroup() {
String query ="SELECT DISTINCT date FROM events";
return mDb.rawQuery(query,null);
}
public Cursor fetchChildren(String date) {
Cursor c = mDb.query(EVENT_TABLE, new String[] { EVENT_ROWID,
EVENT_NAME }, EVENT_DATE + "='" + date + "'" ,
null, null, null, null);
return c;
}
groupCursor.moveToFirst();