It is necessary to constantly update the list of chats in the ListView and the user can constantly scroll through this list. Update doing in

  private TimerTask timerTask = new TimerTask() { @Override public void run() { if(isLoading) return; arrayChat.clear(); loadChatList(((BaseActivity)getActivity()).login, ((BaseActivity)getActivity()).key, limit,0,""); } }; 

The whole problem is arrayChat.clear() . If the user is currently scrolling the list, then on this line an error IndexOutOfBoundsException: Index: 58, Size: 0 appears.

How can it be easier to beat if you do not make a function that will update the list without zeroing? Or not to do without this function?

  • And why is ListView selected at all? It is generally better not to use it, and even more so when there is the word "UPDATE". Instead of ListView, it is better to use the new widget - RecyclerView. - Ksenia
  • Yes, thank you, it's time. I will rewrite. - yavafree

1 answer 1

It turned out to be easier to write the following))

 int index = arrayChat.indexOf(chat); if(index>-1) { if(index!=j) { arrayChat.remove(chat); arrayChat.add(j, chat); } } else arrayChat.add(chat); 

Thanks to all.