【问题标题】:ArrayList getting changed without any operation on itArrayList 在没有任何操作的情况下被更改
【发布时间】:2018-04-14 19:54:28
【问题描述】:

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

谁能指导我克服这个问题。

注意

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

【问题讨论】:

    标签: android arraylist android-edittext adapter addtextchangedlistener


    【解决方案1】:

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

    代替:

    companyList = getCompanyList();
    backupCompanyList = companyList;
    

    使用

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

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多