3

我正在使用自定义 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 更好的方法来执行数据库操作?

日志是:

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)
4

2 回答 2

0

您是否可能不会随着更改getCount()中的数据更新您的价值Cursor?似乎它正试图移动到不再存在的行。

于 2011-03-07T22:08:04.050 回答
-1

我有一个类似的问题。我通过删除代码中的一行来解决它:

mCursorAdapter.notifyDataSetChanged();
于 2014-12-04T15:33:11.767 回答