如何在不丢失滚动位置的情况下更新ArrayAdapter?
这就是我目前所做的。我在ArrayAdapter子类中创建了一个update方法,它执行以下操作:
public void update(List<MyEntity> entities) {
for (MyEntity entity : entities) {
int position = this.getPosition(entity);
if (position >= 0) {
final MyEntity outdateEntity = this.getItem(position);
this.insert(entity, position);
this.remove(outdateEntity);
} else {
this.insert(entity, 0);
}
}
}
public int getPosition(MyEntity updatedEntity) {
for (int i = 0; i < this.getCount(); i++) {
if (this.getItem(i).getId() == updatedEntity.getId()) {
return i;
}
}
return -1;
}然而,这似乎非常低效且容易出错。有没有更好的方法?
发布于 2010-12-07 05:06:48
为什么不首先扩展BaseAdapter呢?您只需在内部更新列表,然后调用notifyDataSetChanged()... :-)
https://stackoverflow.com/questions/4370612
复制相似问题