ArrayList 在没有任何操作的情况下被更改

ArrayList getting changed without any operation on it

我正在尝试在 Android 中编写登录名。逻辑是我在 PopupWindow 的构造函数中启动 ArrayList。在那个 PopupWindow 中,我通过将这个 ArrayList 传递到 Adapter Class 的构造函数来显示一个使用 RecyclerView 的列表。在该列表中,我使用 EditText 使用 addTextChangedListener.

搜索列表

代码如下,

MainActivity.Java

ArrayList<CompanyModel> companyList , backupCompanyList;
CompanyAdapter companyAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

 //initialisation of both arraylists
    companyList = getCompanylist();
    backupCompanyList = companyList;

}

 // inner class declared in the MainActivity.java
public class ShowCompanyData{

    public ShowCompanyData(){

      //initialise popupwindow
      //get view of recyclerview and other view components of the popupwindow , and setadapter to the recyclerview

       companyAdapter = new CompanyAdapter(context , companyList );

       et_search.addTextChangedListener(new TextWatcher() {

                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {

                        String text = et_search.getText().toString().toLowerCase(Locale.getDefault());
                        companyAdapter.filter(text);

                }
            });
    }
 }

  //this button belongs to the Layout file of MainActivity.
  public void showPopupList(View v){
     // this is a button click where i am showing the company list popupwindow
       companyListPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
  }

CompanyAdapter.java

public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.ViewHolder> {


Context context;
ArrayList<CompanyModel> mainArrayList;
ArrayList<CompanyModel> list;

 // other imp methods are also written , but not shown because not necessary to show here

    public CompanyAdapter(Context context, ArrayList<CompanyModel> mainArrayList) {

       this.context = context;
       this.mainArrayList = mainArrayList;
       this.list = new ArrayList<>();
       this.list.addAll(mainArrayList);

    }


    public void filter(String charText) {

        charText = charText.toLowerCase(Locale.getDefault());
        mainArrayList.clear();

        if (charText.length() == 0) {

            mainArrayList.addAll(list);

        } else {

           for (CompanyModel wp : list) {

            if (wp.getCompanyName().toLowerCase(Locale.getDefault()).contains(charText)) {

                mainArrayList.add(wp);

            }

           }
        }   

    notifyDataSetChanged();

    }

}

我面临的问题是,当我在显示公司列表的 PopupWindow 的 EditText 中搜索某些内容时,ArrayList backupCompanyList 被修改为与 companyList ArrayList 相同。

我的问题是,我没有为 backupCompanyList 分配任何内容,也没有将其作为参数传递给适配器 Class,但当我调试应用程序时,backupCompanyList 显示的内容与 companyList 相同,之后在 EditText 中搜索任何内容。
其中 backupCompanyList 应包含 OnCreate 中分配的数据(未更改)并且不应修改更改,因为整个程序中没有对 backupCompanyList 进行任何操作或分配。

谁能指导我解决这个问题。

:

  1. 我没有写完整的代码,我只写了必要的代码
  2. 在将任何文本输入到搜索的 EditText 之前,两个 ArrayList(companyList 和 backupCompanyList)都显示了正确的数据。搜索后才出现问题。

在您的 Activity 的 onCreate 方法中,您正在将 companyList 引用分配给 backupCompanyList 引用。 companyListbackupCompanyList 指的是从 getCompanyList() 方法返回的同一个 ArrayList 对象引用。这就是为什么,它反映了两个列表正在一起变化。实际上,只有一个 ArrayList 对象。

而不是:

companyList = getCompanyList();
backupCompanyList = companyList;

使用

companyList = getCompanyList();
backupCompanyList = new ArrayList<>(companyList);