반응형
안드로이드 listView에서 데이타셋이 변경되는 경우 adapter.notifyDataSetChanged()를 호출하면 자동으로 화면이 갱신됩니다.
그런데 안되는 케이스가 있습니다.
초기 데이타셋이 0건에서 신규 추가 변경되는 경우는 adapter.notifyDataSetChanged()이 동작하지 않습니다.
adapter.notifyDataSetChanged() 메소드를 찾아보면 이렇게 생겼습니다.
mObservers.size() 이 0인 경우는 for 문을 타지 않습니다.
public void notifyChanged() {
synchronized(mObservers) {
// since onChanged() is implemented by the app, it could do anything, including
// removing itself from {@link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {@link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onChanged();
}
}
}
마땅히 해결 방법이 없어서 그냥 화면 refresh 하는 방법으로 해결 하였습니다.
// 초기 데이타셋 목록의 개수
int myListCnt = myList.size();
// 데이타셋 목록 구성
myList.clear();
myList.add(item);
// 초기 데이타셋이 0에서 아이템이 신규 추가될 때에는 화면 리프레시
if(myListCnt == 0) {
((FragmentActivity) context).getSupportFragmentManager().beginTransaction().detach(fragment).commit();
((FragmentActivity) context).getSupportFragmentManager().beginTransaction().attach(fragment).commit();
}
// 그 외에는 notify로 화면 갱신
else {
myAdapter.notifyDataSetChanged();
}
더 좋은 방법 있으면 답글 남겨 주세요.
반응형
'개발 - 안드로이드' 카테고리의 다른 글
안드로이드 Fragment에서 windowSoftInputMode 바꾸는 방법 (0) | 2024.06.18 |
---|---|
안드로이드 fragment에서 editText.requestFocus() 안되는 경우 (0) | 2023.10.11 |
안드로이드 ListView 멀티 아이템 (0) | 2023.10.03 |
안드로이드 raw 이미지 Uri 얻어오기 (0) | 2023.10.03 |
안드로이드 ListView에 ImageView 이미지 넣기 (0) | 2023.10.03 |