0

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.

OptimusPrime
  • 777
  • 16
  • 25
  • There probably is an error being caught in your catch block. Avoid using e.printStackTrace() to log your errors. Instead use Log.e(Tag, "message" +e) . Then see what the error message is – lyncx May 19 '21 at 06:09
  • Just modified the catch block like "Log.d("EXCEPTION", e.toString());", it doesn't seem to reach that line of code. It definitely fails at the notifiyDataSetChanged call though as if I log after that line that doesn't output anything. – OptimusPrime May 19 '21 at 06:31
  • Change `JSONException e` to `Exception e` as well, since you're only catching exception related to JSON, and the issue is probably something else. – lyncx May 19 '21 at 06:47
  • Thanks, I just realised that. I will change it and check the exception. I have to start work now so this will be about 10 hours from now. – OptimusPrime May 19 '21 at 06:50
  • I have capture the exception and updated the description. – OptimusPrime May 19 '21 at 17:46
  • I've solved it with the post below, shall I delete this question from Stack Overflow? https://stackoverflow.com/questions/23212904/ibeacon-android-library-0-7-6-upgrade-issue – OptimusPrime May 19 '21 at 17:54
  • What was the error through? – lyncx May 20 '21 at 00:41
  • Can't remember the exact error, but it relates to trying to update a view on a thread which is not the main one. – OptimusPrime May 29 '21 at 06:31

0 Answers0