简体   繁体   中英

How to reset a List to its initial state

I am a beginner when it comes to AndroidStudio and I am developing this app, where I am having problems with filtering information from a firebase database. When clicking on switch1 (activating it), I get the filtered Informations, but when I deactivate it, I don't get a list with all Infos but only the filtered ones added one more time. The part of the code that's supposed to be responsible for that is commented under //delivery service

public class SearchActivity extends AppCompatActivity {
private final FirebaseFirestore db = FirebaseFirestore.getInstance();
private AppModel appModel;

//Datenkategorien
private boolean[] checkedDK;

//Betriebsart
private boolean[] checkedBA;

//Produktgruppe
private boolean[] checkedPr;


private String[] orgaList;

//bzw. Liste aus Organization-Objekte getname().toString
//final Organization[] organisations = ...;
private boolean[] checkedOrgas;
private List<Company> companies;
private ListView listView;
private CompanyAdapter adapter;
private int layout;

@RequiresApi(api = Build.VERSION_CODES.N)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_main);

    layout = R.layout.company_list;
    appModel = new AppModel();

    //Daten von FireStore ablesen und konvertieren
    ParseData parseData = new ParseData(db, appModel);
    parseData.parseFrom();



    //Liste von Betrieben aus dem Datenbank
    companies = appModel.getCompanies();
    

    checkedDK = new boolean[getResources().getStringArray(R.array.dataCategories).length];
    checkedBA = new boolean[getResources().getStringArray(R.array.betriebsarten).length];
    checkedPr = new boolean[getResources().getStringArray(R.array.products).length];
    checkedOrgas = new boolean[4];

    this.listView = findViewById(R.id.companiesList);

    this.adapter = new CompanyAdapter(this, this.layout, this.companies,false);
    this.listView.setAdapter(this.adapter);

    //Swicth, ob alle Informationen gezeigt werden sollen
    SwitchCompat allInfo = findViewById(R.id.switch3);
    allInfo.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if(isChecked) {
            this.adapter.setChecked(true);
            this.layout = R.layout.all_info_company;
        } else {
            this.adapter.setChecked(false);
            this.layout = R.layout.company_list;
        }
        this.adapter.setLayout(this.layout);
        this.adapter.notifyDataSetChanged();

    });

    //delivery service
    SwitchCompat deliveryService = findViewById(R.id.switch1);
    deliveryService.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if(isChecked) {
            companies.removeIf(company -> !company.hasDeliveryService());
        } else {
            this.companies = this.appModel.getCompanies();

        }
        adapter.clear();
        adapter.addAll(companies);
        adapter.notifyDataSetChanged();
    });

Here is the other class used (not the full code, Company is a self-defined class):

public class AppModel {
List<Company> companies;
List<DataCategory> dataCategories;

List<String> organizationsName;
List<Types> companyTypes;
List<Categories> productCategories;
boolean isOpen;
TimeInterval openingHours;
boolean deliveryService;


public AppModel(){
    this.companies = new ArrayList<>();
    this.dataCategories = new ArrayList<>();
    this.organizationsName = new ArrayList<>();
    this.companyTypes = new ArrayList<>();
    this.productCategories = new ArrayList<>();
    this.isOpen = false;
    this.deliveryService = false;
}



public List<Company> getCompanies() {
    return companies;
}

I tried creating another ArrayList temp = new ArrayList(); and then temp.addAll(companies) and only after that altering companies and by else clearing companies and then addAll(temp) but it didn't work

Looks like you are trying to use same AppModel instance when you uncheck. That's why you got already filtered value again. try this code instead.

deliveryService.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if(isChecked) {
            appModel = new AppModel();
            companies = appModel.getCompanies();
            companies.removeIf(company -> !company.hasDeliveryService());
        } else {
            appModel = new AppModel();
            companies = appModel.getCompanies();
           
        }
        adapter.clear();
        adapter.addAll(companies);
        adapter.notifyDataSetChanged();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM