ArrayAdapter#notifyDataSetChangedのソースコードを見たことありますか??
1 2 3 4 5 6 7 8 | /** * {@inheritDoc} */ @Override public void notifyDataSetChanged() { super .notifyDataSetChanged(); mNotifyOnChange = true ; } |
1 2 3 4 | /** * Notifies the attached observers that the underlying data has been changed * and any View reflecting the data set should refresh itself. */ |
データ変更をオブザーバに通知してViewに反映するメソッドですよね。ご存知の通り。
でも僕の疑問はなぜ、
mNotifyOnChange = true;
があるのかということなんですよねー。
notifyDataSetChangedを呼ぶとフラグがtrueにされてしまう。そのフラグはsetNotifyOnChangeというメソッドで操作できる。
Control whether methods that change the list (
add(T)
, insert(T, int)
, remove(T)
, clear()
) automatically call notifyDataSetChanged()
.個人的に複数のデータを仕方なくfor文で登録しようとすると
1. setNotifyOnChange(false)
2. for文内でaddやinsertを複数回行う
3. setNotifyOnChange(true)
4. notifyDataSetChanged
というイメージで実装するのかなと思っていたのに。。いや、これで正しいとは思うんですが、notifyDataSetChangedを一度呼び出してしまうとfalseになっていたものがtrueになってしまうので注意が必要だというお話でした。
なぜこのようになっているのか疑問。。。誰か教えてください。。。