开发者

Updating SQLite database in background thread on Android

I'm using a custom CursorAdapter to display data stored in a database. The data is pulled from a server, parsed, and inserted into the database in a separate thread. Because I don't want to keep around any old data, I delete all rows from the table, then insert each row as I parse the information in the server response. If I scroll around in the ListActivity, I sometimes get a crash, apparently, when the table is cleared, and CursorAdapter getView or bindView are trying to query the cursor.

It is NOT my custom CursorAdapter which fails, I've already handled the RuntimeException that can happen when I use getInt() or getString().

From what I've read, serialization of queries is dependent on how you 开发者_如何学Gouse SQLiteOpenHelpers. I've created a custom SQLiteOpenHelper, similar to the notepad example in the Android demos.

When I update my db I use the following code snippet:

ContentResolver cr = ctx.getContentResolver();
ContentValues values = new ContentValues();
String response = _getUrlResponse(url);
cr.delete(tableName);
values.put(parseString(response));
cr.insert(tableName, values);

Is there a better way to perform the db operations than by using a ContentResolver?

The logcat is:

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)


Are you possibly not updating your getCount() value as the data in your Cursor changes? It seems like it is trying to move to a row that no longer exists.


I had a similar issue. I solved it by removing a line I had in my code:

mCursorAdapter.notifyDataSetChanged();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜