如何在不丢失滚动位置的情况下更新ArrayAdapter
?
这就是我目前的做法。我在ArrayAdapter
子类中创建了一个更新方法,它执行以下操作:
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;
}
然而,这似乎非常低效且容易出错。还有更好的方法吗?