Updating SQLite database in background thread on Android

I am using a custom CursorAdapter to display data stored in a database. Data is retrieved from the server, analyzed and inserted into the database in a separate stream. Since I do not want to store old data, I delete all rows from the table and then insert each row when analyzing the information in the server response. If I browse in ListActivity, I sometimes get a crash, apparently when the table is cleared and the CursorAdapter getView or bindView try to query the cursor.

This is not my custom CursorAdapter that fails, I have already handled a RuntimeException that can occur when I use getInt () or getString ().

From what I read, query serialization depends on how you use SQLiteOpenHelpers. I created my own SQLiteOpenHelper, similar to the example notepad in demos of Android.

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 db operations than using ContentResolver?

Logarithm:

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)
+3
source share
2 answers

Perhaps you are not updating the value getCount()as data in your changes Cursor? It seems to be trying to move to a line that no longer exists.

0
source

I had a similar problem. I decided to remove it from the line that I had in my code:

mCursorAdapter.notifyDataSetChanged();
-1

All Articles