I have a listView whixh is refresh each time the client (my android app) receive a message from the server but the problem is when i click to many time on the refresh button the app crash and send me this error: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.
edit 1: the crash does not happen every time it onlu happen when i click about ten times quickly on the refresh button
activity code:
class receiveimplements Runnable {
@Override
public void run() {
try {
if (socket != null && socket.isConnected()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
for (; ; ) {
while ((textRecu = reader.readLine()) != null) {
if (textRecu.length() < 6) {
bug = true;
break;
}
Log.d("textrecuapres", textRecu);
index++;
if (index == 1) {
listTitre.add(0, textRecu);
sendListeAndMakeAffichage(listSource,listTitre);
} else if (index == 2) {
listSource.add(0, textRecu);
index = 0;
}
}
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void sendListeAndMakeAffichage(final List<String> sourceList,final List<String> sourceTitre) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fragmentListe.setListTitreAndListSource(sourceTitre, sourceList);
}
});
}
Listfragment code:
public void setListTitreAndListSource(List<String> listTitre, List<String> listSource) {
this.listTitre = listTitre;
this.listSource = listSource;
adapter.bind(listTitre);
setListAdapter(adapter);
}
adapter code:
public void bind(List<String> model) {
notifyDataSetChanged();
listeTitre = model;
notifyDataSetChanged();
}