I'm trying to do a chat application with two activities. On the first plan is a short list containing the last sent message in each row. A second activity contain all conversation. I use socket.io and my problem is that, when I click back button and then I come back to my app notifyDataSetChanged() stops working. My app recevie a messages from dialog box from a website which a cooperates with android app. In console log I see that a messages from a website are received and onTaskComplete() method is called but a listview is not refreshing. I read that an adapter loses a reference to a listview after restart an activity but how to see in my code I create a new adapter in onResume() method. I don't understand what I do wrong? While if I will throw out beyond the condition "mSocket.on("message", onMessage);" then a listview is refresh after come back to app but a messages are multiple as many times as there were returns.
below is my code, please help!
MainActivity:
package com.example.seadog.fb_dialog;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends Activity implements MyListener {
public ListView listView;
public MyBaseAdapter adapter;
public TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Get Socket.io Object
*/
SocketIO socketIo = new SocketIO();
Socket mSocket = socketIo.getSocket(); // get socket
Integer id = socketIo.getId(); // get Website ID
if(mSocket == null) {
socketIo.Connection();
mSocket = socketIo.getSocket();
mSocket.on("message", onMessage);
}
listView = (ListView) findViewById(R.id.listView);
/*
* OnItemClickListener
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Conversation.class);
intent.putExtra("item", position);
startActivity(intent);
TextView textView = (TextView) view.findViewById(R.id.descitem);
textView.setTypeface(null, Typeface.NORMAL);
}
});
textView = (TextView) findViewById(R.id.count);
}
private Emitter.Listener onMessage = new Emitter.Listener() {
/*
* Message Listener
*/
@Override
public void call(Object... args) {
Boolean isset = false;
try {
JSONObject object = (JSONObject) args[0];
String _id = object.getString("_id");
String message = object.getString("message");
JSONObject obj = new JSONObject();
obj.put("direction", "fb-lt");
obj.put("message", message);
obj.put("date", "2017-05-29T12:15:49.245Z");
for(int i = 0; i < arrayList.size(); i++){
ListData ld = (ListData) arrayList.get(i);
String id = ld.getId();
if(_id.equals(id)){
JSONArray Data = ld.getData();
Data.put(obj);
ld.setDescription(message);
arrayList.set(i, ld);
isset = true;
Log.d("LOG", message);
}
}
if(!isset) {
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj);
ListData ld = new ListData();
ld.set_id(_id);
ld.setID(1);
ld.setTitle("Klient:");
ld.setDescription(message);
ld.setData(jsonArray);
arrayList.add(ld);
}
onTaskComplete();
} catch (JSONException e) {
e.printStackTrace();
}
}
};
public void onResume() {
super.onResume();
ArrayList clone = (ArrayList) arrayList.clone();
arrayList.clear();
arrayList.addAll(clone);
adapter = new MyBaseAdapter(this, arrayList);
listView.setAdapter(adapter);
}
@Override
public void onTaskComplete() {
runOnUiThread(new Runnable() {
@Override
public void run () {
Log.d("LOG:","refresh");
adapter.notifyDataSetChanged();
}
});
}
}
Interface MyListener:
package com.example.seadog.fb_dialog;
import java.util.ArrayList;
public interface MyListener {
ArrayList arrayList = new ArrayList();
void onTaskComplete();
}