2011-03-07 63 views
3

我正在使用自定義的CursorAdapter來顯示存儲在數據庫中的數據。數據從服務器中提取出來,進行解析,然後在單獨的線程中插入到數據庫中。由於我不想保留任何舊數據,因此我從表中刪除所有行,然後在解析服務器響應中的信息時插入每行。如果我在ListActivity中滾動,顯然有時會導致崩潰,當表被清除時,CursorAdapter getView或bindView正在嘗試查詢遊標。在Android上的後臺線程中更新SQLite數據庫

這不是我的自定義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

可能是很好反正張貼光標代碼 – 2011-03-07 22:08:21

+0

我應該說,我正在重寫的CursorAdapter的唯一功能是NewView的()和bindView()。至於利用getCount()的建議,也許我可以做這樣的事情: '@覆蓋 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司){ \t \t嘗試{ \t \t \t回報super.getView(位置,convertView,父母); \t \t} catch(RuntimeException e){ \t \t \t e.printStackTrace(); \t \t \t super.getCursor().requery(); \t \t \t return newView(parent.getContext(),super.getCursor(),parent); \t \t} \t}' – JustinJ 2011-03-07 22:34:44

回答

0

你可能不更新getCount()值作爲你的Cursor變化的數據?似乎它試圖移動到不再存在的行。

-1

我有類似的問題。我解決它通過去除一條線,我在我的代碼:

mCursorAdapter.notifyDataSetChanged();