Search code examples
androidarraylistandroid-edittextadapteraddtextchangedlistener

ArrayList getting changed without any operation on it


I am trying to write a login in Android. The Logic is I am initiating an ArrayList in a constructor of a PopupWindow. In that PopupWindow I am showing a list using RecyclerView, by passing this ArrayList into the constructor of the Adapter Class. In that list I am using an EditText to search the list using addTextChangedListener.

The code is as follows,

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();

    }

}

Here the issue I am facing is, when I search something in in the EditText of PopupWindow where the list of Company's is shown, the ArrayList backupCompanyList is getting modified same as the companyList ArrayList.

My question is, I am not assigning anything to the backupCompanyList, also not passing it as a parameter to the Adapter Class, still when I debug the app the backupCompanyList are showing same contents as companyList, after searching anything in the EditText.
Where the backupCompanyList should contains the data (unchanged) assigned in OnCreate and should not modify the changes, because there are no operations or assignments done to the backupCompanyList in the entire program.

Can anyone guide me to overcome this issue.

Note :

  1. I have not written full code, I have written only necessary code
  2. Both ArrayLists (companyList & backupCompanyList) are showing proper data before entering any text into EditText of search. And issue is occuring only after searching.

Solution

  • In your Activity's onCreate method, your are assigning companyList reference to backupCompanyList reference. Both companyList and backupCompanyList are referring to the same ArrayList object reference returned from getCompanyList() method. That's why, it's reflecting both lists are changing together. In actual, there's only one ArrayList object.

    Instead of:

    companyList = getCompanyList();
    backupCompanyList = companyList;
    

    Use

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