我有这个数组类,在重新运行recyclerview后不会删除项目

问题描述 投票:0回答:1

下面的类在数组中使用。

package com.example.ShivitoMGO;

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

主要活动

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;

在此应用程序中,回收者视图创建“房间”,如果选择了房间,则将textview和其他2个值放入RoomTable。它们被存储并用于服务中,以检查其他以太值是否变化。除非我使用刷卡刷新的女巫再次运行回收器视图,否则一切都会按预期进行。如果我没有刷新并且在回收视图中选择了相同的项目,它将从CountCheck中删除它。但是,如果我运行刷新并选择我之前选择的相同的recycler-view项目,它将添加它而不是将其删除。这对我没有影响,因为我使用了for循环来检查CountCheck.get(i).RoomName(又名textview),如果名称相同,则将删除if语句,而不是add。有可能以某种方式结束2个CountCheck对象吗?同名????请我没有关于这个的想法。谢谢。

android-studio android-recyclerview onpause onsaveinstancestate custom-arrayadapter
1个回答
0
投票

我不记得为什么。也许有人可以解释,但我改变了这一行

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

为此

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

这解决了问题。如果您正在阅读本文,请告诉我有什么不同。

© www.soinside.com 2019 - 2024. All rights reserved.