我的代码
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();
(在 * HERE * 上面指出)我的应用程序在上下文菜单中按删除时崩溃,没有它,它可以正常工作。但是当我更新一个条目时,它没有注意到变化......那么为什么当我按删除时这段代码会相互影响?以及如何使这项工作?
修改
我发现要让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();
}
现在它更新了,但是在删除时我的应用程序再次崩溃...当我从this.adapter.notifyDataSetChanged();
函数中删除deleteItem
部分时它不会崩溃但它不会刷新我的列表视图要么...
崩溃之前的日志:
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):强制完成 activity com.example.NutritionSeeker / .HistoryActivity 08-02 06:13:48.488:WARN / ActivityManager(112):活动暂停超时 HistoryRecord {44f54130 com.example.NutritionSeeker / .HistoryActivity}
答案 0 :(得分:0)
我不知道rep.getAllEntries做了什么,但你应该使用AsyncTaskLoader http://developer.android.com/reference/android/content/AsyncTaskLoader.html和附加的适配器。如果您可以提供帮助,请创建一个ContentProvider作为您的存储库,并使用CursorLoader代替http://developer.android.com/reference/android/content/CursorLoader.html。