【问题标题】:How to reset a List to its initial state如何将列表重置为其初始状态
【发布时间】:2021-05-22 17:30:59
【问题描述】:

我是 AndroidStudio 的初学者,我正在开发这个应用程序,我在过滤来自 firebase 数据库的信息时遇到问题。单击 switch1(激活它)时,我得到过滤的信息,但是当我停用它时,我没有得到一个包含所有信息的列表,而只有过滤的信息又添加了一次。应该负责的代码部分在 //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();
    });

这里是使用的其他类(不是完整代码,Company是自定义类):

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;
}

我尝试创建另一个 ArrayList temp = new ArrayList();然后是 temp.addAll(companies) 并且只有在那之后改变公司并通过其他清算公司然后 addAll(temp) 但它没有工作

【问题讨论】:

  • 您正在从公司列表中删除条目。想办法。你面前有 10 个盒子。您删除了 5. 现在要完成列表,您需要添加回删除的框,您可以保留完整的副本以替换为原始状态
  • @KaranjotSingh 是的,我知道问题出在 removeIf 上,但是如何在不删除元素的情况下过滤列表,我尝试使用公司的副本,但它也不起作用
  • 在您停用用该副本替换公司时保留完整列表的副本
  • @KaranjotSingh 你介意更具体一些吗,因为我尝试了很多方法来复制,但没有一个奏效。
  • 使用这个而不是删除。 this.companies = appModel.getCompanies().stream().filter(company -> !company.hasDeliveryService()).collect(Collectors.toList())

标签: java android firebase android-studio filter


【解决方案1】:

取消选中时,您似乎正在尝试使用相同的 AppModel 实例。这就是为什么您已经再次获得过滤值的原因。试试这个代码。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2021-09-05
    • 2016-07-10
    • 2015-02-21
    • 2013-03-27
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多