我有一个事件名称和日期的数据库。我正在尝试使用将按事件日期分组的事件创建一个expandablelistview。ExpandableListView sqlite错误
我没有找到很多与我的情况相关的信息。 SDK中的例子是使用内容提供者,但我还没有读取/尝试过,并且我已经用旧的方式设置了数据库帮助器。
我发现我认为是一个很好的例子here并试图使它适应我的使用,但它崩溃了,我无法弄清楚我做错了什么。
我的代码
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()方法如下(只是为了检查我的光标/列):
mGroupsCursor = mDbHelper.fetchGroup();
startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();
String Text1 = mGroupsCursor.getString(mGroupsCursor.getColumnIndex(AttendanceDB.EVENT_DATE));
Toast.makeText(this, Text1, Toast.LENGTH_LONG).show();
Cursor childCursor = mDbHelper
.fetchChildren(mGroupsCursor.getString(mGroupsCursor.getColumnIndex(AttendanceDB.EVENT_DATE)));
startManagingCursor(childCursor);
childCursor.moveToFirst();
String Text2 = childCursor.getString(childCursor.getColumnIndex(AttendanceDB.EVENT_NAME));
Toast.makeText(this, Text2, Toast.LENGTH_LONG).show();
祝酒词输出预期的事件日期和事件名称的数据,所以我在亏损为我出错了。
其他(也许)相关代码:
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;
}
任何帮助将不胜感激。
我会在一堆圣经上发誓,我通过这个链接读了三次,并错过了那个关于_id被要求的位。现在正在工作。现在我只需要让它在一个片段中工作。 :p但是,如果我遇到问题,这可能是另一个问题。感谢您的协助! – Barak 2012-03-20 02:03:59