我的代碼神祕Android和notifyDataSetChanged()
public void deleteItem(int position) {
this.rep.open();
this.rep.deleteEntry(this.adapter.getObject(position));
this.adapter.deleteObjectFromList(position);
this.rep.close();
this.adapter.notifyDataSetChanged();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.update:
callUpdateForm(adapter.getObject(info.position));
this.rep.open();
this.adapter.swapItems(this.rep.getAllEntries());
this.rep.close();
*** HERE ***
return true;
case R.id.delete:
deleteItem(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
如果我把this.adapter.notifyDataSetChanged();
(在*這裏*上面指出)我的應用程序崩潰,當我按下刪除上下文菜單上,如果沒有它,它的工作原理精細。但是當我更新一個條目時,它不會注意到這個改變......那麼爲什麼當我按刪除時這段代碼會相互作用呢?以及如何使這項工作?
編輯
我要指出的是ListView控件刷新其自上更新我需要做的是在onResume()
事件
所以現在我的代碼:
public void deleteItem(int position) {
this.rep.open();
this.rep.deleteEntry(this.adapter.getObject(position));
this.adapter.deleteObjectFromList(position);
this.rep.close();
this.adapter.notifyDataSetChanged();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.update:
callUpdateForm(adapter.getObject(info.position));
return true;
case R.id.delete:
deleteItem(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
@Override
public void onResume() {
super.onResume();
this.rep.open();
this.adapter.swapItems(this.rep.getAllEntries());
this.adapter.notifyDataSetChanged();
this.rep.close();
}
現在更新,但刪除我得到我的應用程序再次崩潰...當我從deleteItem
刪除this.adapter.notifyDataSetChanged();
部分功能它不會崩潰,但它不刷新我的列表視圖...
原木之前CRASH:
08-02 06:13:47.566: INFO/System.out(5788): Comment deleted with id: 10
08-02 06:13:47.906: DEBUG/AndroidRuntime(5788): Shutting down VM
08-02 06:13:47.906: WARN/dalvikvm(5788): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-02 06:13:47.916: ERROR/AndroidRuntime(5788): FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at com.example.NutritionSeeker.Adapters.EntryAdapter.getView(EntryAdapter.java:65)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
at android.widget.ListView.onMeasure(ListView.java:1109)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
08-02 06:13:47.926:WARN/ActivityManager(112):強制結束 活性com.example.NutritionSeeker/.HistoryActivity 08-02 06: 13:48.488:WARN/ActivityManager(112):用於 HistoryRecord {44f54130 com.example.NutritionSeeker/.HistoryActivity}活動暫停超時
任何logcat?將有幫助 –
兄弟在notifydatasetchnge方法 –
下面寫this.rep.close它沒有幫助:( –