我正在使用自定义CursorAdapter来显示存储在数据库中的数据。数据从服务器中提取,解析并在单独的线程中插入数据库。因为我不想保留任何旧数据,所以我从表中删除所有行,然后在解析服务器响应中的信息时插入每一行。如果我在ListActivity中滚动,显然,当表被清除时,我有时会崩溃,而CursorAdapter getView或bindView正试图查询光标。
我的自定义CursorAdapter失败了,我已经处理了当我使用getInt()或getString()时可能发生的RuntimeException。
根据我的阅读,查询的序列化取决于您使用SQLiteOpenHelpers的方式。 我已经创建了一个自定义的SQLiteOpenHelper,类似于Android演示中的记事本示例。
当我更新我的数据库时,我使用以下代码片段:
ContentResolver cr = ctx.getContentResolver();
ContentValues values = new ContentValues();
String response = _getUrlResponse(url);
cr.delete(tableName);
values.put(parseString(response));
cr.insert(tableName, values);
有没有比使用ContentResolver更好的方法来执行数据库操作?
logcat是:
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): java.lang.IllegalStateException: couldn't move cursor to position 17
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.CursorAdapter.getView(CursorAdapter.java:178)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.AbsListView.obtainView(AbsListView.java:1460)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.ListView.makeAndAddView(ListView.java:1809)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.ListView.fillUp(ListView.java:764)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.ListView.fillGap(ListView.java:710)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3421)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.AbsListView.onTouchEvent(AbsListView.java:2301)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.widget.ListView.onTouchEvent(ListView.java:3621)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.view.View.dispatchTouchEvent(View.java:3823)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1723)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1129)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1707)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.os.Looper.loop(Looper.java:123)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at java.lang.reflect.Method.invoke(Method.java:521)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-07 15:35:07.351: ERROR/AndroidRuntime(15826): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您是否可能因getCount()
更改中的数据而更新Cursor
值?似乎它试图移动到不再存在的行。
答案 1 :(得分:-1)
我有类似的问题。我通过删除我的代码中的一行来解决了这个问题:
mCursorAdapter.notifyDataSetChanged();