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

时间:2018-04-14 19:54:29

标签: android arraylist android-edittext adapter addtextchangedlistener

我正在尝试在Android中编写登录信息。逻辑是我在PopupWindow的构造函数中启动ArrayList。在PopupWindow中,我使用RecyclerView显示一个列表,方法是将此ArrayList传递给Adapter Class的构造函数。在该列表中,我使用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分配任何内容,也没有将它作为参数传递给Adapter Class,当我调试app时,backupCompanyList在editText中搜索任何内容后显示与companyList相同的内容。
其中backupCompanyList应包含OnCreate中分配的数据(未更改),并且不应修改更改,因为整个程序中没有对backupCompanyList执行任何操作或分配。

任何人都可以指导我克服这个问题。

注意

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

1 个答案:

答案 0 :(得分:0)

在您的活动的onCreate方法中,您正在为companyList引用分配backupCompanyList引用。 companyListbackupCompanyList都指的是从getCompanyList()方法返回的相同ArrayList对象引用。这就是原因,它反映了这两个名单正在一起变化。实际上,只有一个ArrayList对象。

而不是:

companyList = getCompanyList();
backupCompanyList = companyList;

使用

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