Search code examples
android-studioandroid-recyclerviewonpauseonsaveinstancestatecustom-arrayadapter

I have this array class that is not removing items after re-running my recyclerview


The below class is used in an array.

package com.example.ShivitoMGO;

public class RoomTable {
    public String RoomName,UpDown,minmaxint;
}

Main Activity

static ArrayList <RoomTable> CountCheck = new ArrayList<>();

    public void playerup(View vvv){

        st_spinner = v1.findViewById(R.id.spinner);
        st_reportLayout = v1.findViewById(R.id.reportlayout);
        st_Leanervidimg = v1.findViewById(R.id.Linearvidcopy);
        TextView roomname = v1.findViewById(R.id.action_Players1);
        RoomTable roomtb = new RoomTable();
        if (CountCheck.size() == 0){
            //playerupdown = "up";

            Toast.makeText(this, "Will notify when " + mRooms.get(position1) + " players increase  #" + mRoomSize.get(position1), Toast.LENGTH_LONG).show();
            String[] minmaxval = mRoomSize.get(position1).split("/");
            CountCheckint = Integer.parseInt(minmaxval[0].trim());
            //CountCheck = (String) mRooms.get(position1);

            roomtb.RoomName = (String) mRooms.get(position1);
            roomtb.minmaxint = minmaxval[0].trim();
            roomtb.UpDown = "up";
            Log.d("added: ", "it was added");
            CountCheck.add(roomtb);
            Log.d("RoomTableadd: ",roomtb.RoomName+ " " + roomtb.minmaxint +" " +roomtb.UpDown);
            st_reportLayout.setVisibility(View.GONE);
            Log.d("Longclickhere: ", mRoomSize.get(position1));
            Log.d("RoomNameCount ", String.valueOf(CountCheck.get(0).RoomName));
        }else {
            int exist1 = 0;
            int poss;
            for (int i = 0; i < CountCheck.size(); i++) {
                if (roomname.getText() == CountCheck.get(i).RoomName) {
                    Log.d("RoomNametxt: " , CountCheck.get(i).RoomName);
                    Log.d("RoomNametxt: ", (String) roomname.getText());
                    Toast.makeText(this, "Notification " + CountCheck.get(i).RoomName + " OFF!", Toast.LENGTH_LONG).show();
                    Log.d("Removed item: ", String.valueOf(CountCheck.size()));
                    CountCheck.remove(i);
                    Log.d("Removed item: ", String.valueOf(CountCheck.size()));
                    st_reportLayout.setVisibility(View.GONE);
                    exist1 = 1;
                    poss = i;
                } else {
                    exist1 = 0;
                }
            }
            if (exist1 == 0) {
                Toast.makeText(this, "Will notify when " + mRooms.get(position1) + " players increase  #" + mRoomSize.get(position1), Toast.LENGTH_LONG).show();
                String[] minmaxval = mRoomSize.get(position1).split("/");
                CountCheckint = Integer.parseInt(minmaxval[0].trim());
                //CountCheck = (String) mRooms.get(position1);

                roomtb.RoomName = (String) mRooms.get(position1);
                roomtb.minmaxint = minmaxval[0].trim();
                roomtb.UpDown = "up";
                Log.d("added: ", "it was added");
                CountCheck.add(roomtb);
                Log.d("RoomTableadd: ", roomtb.RoomName + " " + roomtb.minmaxint + " " + roomtb.UpDown);
                st_reportLayout.setVisibility(View.GONE);
            }





            Log.d("CountSize: ", String.valueOf(CountCheck.size()));
            for (int xb = 0;xb<CountCheck.size();xb++) {
                try {
                    Log.d("RoomNameCount ", String.valueOf(CountCheck.get(xb).RoomName));


                } catch (Exception e) {
                    Log.d("Out of range ", String.valueOf(e));
                }
            }

        }
    }

Recycler-View

            @Override
            public boolean onLongClick(View v) {
                // Launch your web intent
                if (CountCheck.size() != 0){
                    Log.d("Longclickhere: ",mRoomSize.get(position));
                    Toast.makeText(mContext, mRoomSize.get(position), Toast.LENGTH_SHORT).show();
                    Toast.makeText(mContext, "Will notify when "+mRooms.get(position)+" players decrease", Toast.LENGTH_SHORT).show();
                    String[] minmaxval = mRoomSize.get(position).split("/");
                    MainActivity.CountCheckint = Integer.parseInt(minmaxval[0].trim());


                } else{
                    //MainActivity.CountCheck = "";
                    Toast.makeText(mContext, "Player Decrease notification OFF", Toast.LENGTH_SHORT).show();
                }
                return true;

In this app the recycler view creates the "rooms" and if the room is selected the textview and 2 other values are put in to the RoomTable. these are stored and used in a service to check if ether of the other to values change. Everything works as intended unless i use the swip-to-refresh witch runs the recycler-view again. If i do not refresh and i select the same item in the recycler-view it will remove it from CountCheck . However if i run the refresh and select the same recycler-view item that i selected previously it will add it instead of removing it. This Makes no since to me because i use a for loop to Check the CountCheck.get(i).RoomName aka the textview and if the names are the same then my if statement will remove instead of add. is it somehow possible i'm ending up with 2 CountCheck Objects????? with the same name???? Please I'm out of ideas on this one. Thanks.


Solution

  • I dont remember why. Maybe someone can explain but i changed this line

    if (roomname.getText() == CountCheck.get(i).RoomName)
    

    To this

    if (roomname.getText().equals(CountCheck.get(i).RoomName));
    

    and that fixed the issue. please let me know the difference if you are reading this.