I am making a chat application which connects to my chat server using wss. The web based version of this application works fine.
I am having trouble when updating the recycler view with the room list which needs to be triggered by the listener. Basically, it is not refreshing the view and it closes the web socket. If I then go in again to the app it will load the items into the recycler view from the first call.
My code is below:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_list);
SharedPreferences sharedPrefs = this.getSharedPreferences(this.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefs.edit();
conn = ServerConn.getInstance(this);
notifier = SockNotifier.getInstance();
roomList = RoomList.getInstance();
//Recycler
Context ctx = this;
recyclerView = findViewById(R.id.rmRecycRooms);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ArrayList<RoomItem> roomsForAdapter = new ArrayList<RoomItem>();
roomRecycler = new RoomRecycler(this,roomList.getRoomList(), conn);
recyclerView.setAdapter(roomRecycler);
roomRecycler.notifyDataSetChanged();
notifier.setListener(new SockNotifier.MessageEventListener() {
@Override
public void onRegister(String id) {
}
@Override
public void onSetName(String name) {
}
@Override
public void onRoomList(String rooms) {
Log.d("ROOM LIST", "Room list triggered");
try {
ArrayList<RoomItem> roomsForAdapter = roomList.loadRooms(rooms);
Log.d("ROOMLIST", String.valueOf(roomList.size()));
roomRecycler.setRoomList(roomsForAdapter);
roomRecycler.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
It is failing on "roomRecycler.notifyDataSetChanged();". I have also tried creating a new instance as well, but that also fails. Can anyone help?
The error it is giving is as below. Is this because the listener parent class is a singleton and is shared between the activities? I am not sure how I can resolve it if that is the case because I need it to work between activities so I can notify them on events from the server.