我在使用set status方法更新数据时单击按钮以获取错误我已将数据列表发送到下一个活动,同时收到错误 我是Android编程新手
ImageButton ibAddMore = (ImageButton) findViewById(R.id.ibAddMore);
ibAddMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataBaseHelper db = new DataBaseHelper(getApplicationContext());
for (People people : alertList) {//In this Line getting error
if (people.getStatus() == 1) {
db.setStatus(people.getId(), "0");
alertList.add(people);
} else {
db.setStatus(people.getId(), "1");
}
}
Intent intent = new Intent(AlertList.this, AlertListAll.class);
startActivity(intent);
}
}
);
设置状态方法
public int setStatus(String peopleId, String status) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(STATUS, status);
return sqLiteDatabase.update(TABLE_PEOPLE, values, ID + "=?",
new String[]{peopleId});
}
例外:
java.util.ConcurrentModificationException
at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:62)
at com.Jaydeep.alertme.activity.AlertList$1.onClick(AlertList.java:67)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5136)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您无法直接修改正在循环播放的同一个集合。但你可以使用ListIterator来做到这一点。 E.g。
for (ListIterator<People> iterator = alertList.listIterator(); iterator.hasNext(); ) {//In this Line getting error
People people = iterator.next();
if (people.getStatus() == 1) {
db.setStatus(people.getId(), "0");
iterator.add(people);
} else {
db.setStatus(people.getId(), "1");
}
}
答案 1 :(得分:0)
您在迭代时将元素添加到同一列表中。它比java还要多。
顺便说一下为什么你想再次将相同的元素添加到你正在迭代的列表中?