Android:IndexOutOfBounds在ListView中删除行时出错
我有一个有2个屏幕的应用程序。 第一个屏幕具有电影的ListView,其中一行由在strings.xml中声明的3个元素组成:标题,日期和总值。 用户可以通过点击菜单按钮来添加电影,该菜单按钮将其发送到另一个屏幕。 第二个屏幕有3个编辑文本对应于标题日期和总量,当它返回到屏幕1时,它按字母顺序排序。
同样,用户也可以通过长按一个上下文菜单的行来编辑/删除条目。 编辑功能像这样工作:
a。)用户长按泰坦尼克号并选择编辑b。)行被删除,用户进入屏幕2 c。)编辑文本由删除的行d中的初始数据填充。)当用户编辑数据时,新电影是添加在ListView的底部。
当用户在ListView的底部删除这部新电影时,就会出现问题。 Logcat给出了一个
java.lang.IndexOutOfBoundsException: Invalid index 50, size is 50
这里是我的代码(注意我使用Perst来保存数据,但我不认为这对我的问题无关紧要):
case R.id.contextedit:
Lab9_082588FetchDetails row = (Lab9_082588FetchDetails) getListView()
.getItemAtPosition(info.position);
Intent editData = new Intent(MovieList.this, Lab9_082588Edit.class);
String startTitle = row.getTitle();
String startGross = row.getGross();
String startDate = row.getDate();
editData.putExtra(Lab9_082588Edit.TITLE_STRING, startTitle);
editData.putExtra(Lab9_082588Edit.GROSS_STRING, startGross);
editData.putExtra(Lab9_082588Edit.DATE_STRING, startDate);
startActivityForResult(editData, MovieList.EDIT_MOVIE);
int posEdit = info.position;
String editTitle = results.get(info.position).getTitle();
results.remove(posEdit);
adapter.notifyDataSetChanged();
//Perst
Index<Lab9_082588FetchDetails> rootEdit = (Index<Lab9_082588FetchDetails>) db
.getRoot();
rootEdit.remove(editTitle, results.get((int) info.id));
db.setRoot(rootEdit);
return true;
编辑类:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection using item.getItemId()
switch (item.getItemId()) {
case R.id.edit:
next();
break;
}
return true;
}
private void next() {
// TODO Auto-generated method stub
EditText movieTitle = (EditText) findViewById(R.id.etTitle);
EditText movieGross = (EditText) findViewById(R.id.etGross);
EditText movieDate = (EditText) findViewById(R.id.etDate);
String title = movieTitle.getText().toString();
String gross = movieGross.getText().toString();
String date = movieDate.getText().toString();
if ((title.length() > 0) && (gross.length() > 0)
&& (date.length() == 4)) {
Intent hobby = getIntent();
hobby.putExtra(Lab9_082588Edit.TITLE_STRING, title);
hobby.putExtra(Lab9_082588Edit.GROSS_STRING, gross);
hobby.putExtra(Lab9_082588Edit.DATE_STRING, date);
setResult(RESULT_OK, hobby);
finish();
}
}
删除功能:
int posDelete = info.position;
String deleteTitle = results.get(
info.position).getTitle();
results.remove(posDelete);
adapter.notifyDataSetChanged();
Index<Lab9_082588FetchDetails> rootDelete = (Index<Lab9_082588FetchDetails>) db
.getRoot();
rootDelete.remove(deleteTitle,
results.get(info.position));
db.setRoot(rootDelete); //Perst
return;
OnActivityResult(编辑):
case EDIT_MOVIE:
Lab9_082588FetchDetails edittedMovie = new Lab9_082588FetchDetails();
NumberFormat formatterEdit = new DecimalFormat("###,###,###");
edittedMovie.setTitle(data
.getStringExtra(Lab9_082588Add.TITLE_STRING));
edittedMovie.setGross("$"
+ formatterEdit.format(Double.parseDouble(data
.getStringExtra(Lab9_082588Add.GROSS_STRING))));
edittedMovie.setDate(data
.getStringExtra(Lab9_082588Add.DATE_STRING));
results.add(edittedMovie);
adapter.notifyDataSetChanged();
填充Listview:
for (int i = 0; i < items.size(); i++) {
Lab9_082588FetchDetails sr = new Lab9_082588FetchDetails();
sr.setTitle(items.get(i).getTitle());
sr.setGross(items.get(i).getGross());
sr.setDate(items.get(i).getDate());
results.add(sr);
Collections.sort(results, ignoreCaseStart);
}
我该如何补救?
发生此问题是因为在删除函数中,首先从结果集合中删除元素(“results.remove(posDelete);”),然后几行后,将“results.get(info.position)”为rootDelete.remove调用获取一个参数,但该参数已被删除。
如果元素是集合的最后一个元素,让我们说第50个元素,“info.position”的值是50.您删除一个元素,所以元素的数量现在是49.在您调用的rootDelete.remove行中results.get(50),它会产生错误。
链接地址: http://www.djcxy.com/p/93163.html上一篇: Android: IndexOutOfBounds Error when deleting row in ListView
下一篇: can't BindViewHolder recyclerview when add header on scroll loard more