0

我在 google/stack 上查看了一些示例,但没有找到任何好的解决方案来解决我的问题。

在显示列表视图的活动中,我得到了一个过滤器(winesCopy 是葡萄酒列表的副本):

public static List<Wine> filterWinesString(List<Wine> wines, Object filterItem)
    {
        for(Wine o : wines)
        {
            if(o.getColor().equals((String)filterItem)||o.getCountry().equals((String)filterItem)||o.getDescription().equals((String)filterItem)||o.getTaste().equals((String)filterItem)||
                    o.getBiologic().equals(filterItem)||o.getSparkling().equals(filterItem)||filterItem.equals(o.getYear()))
            {
                filteredList.add((Wine) o);
            }
        }
        if(!filteredList.equals(null))
        {
            wines= filteredList;
        return wines;
        }
        else{
            return wines;
        }
    }

    public static void clearFilter()
    {
        filteredList.clear();
        wines=winesCopy;
    }

在我用作“对话”的活动中:

Button btnSubmit = (Button) findViewById(R.id.activity_btn_add_filter);

        btnSubmit.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View v) {
                  if(!etLand.equals(null))
                  {
                  ListWineView.filterWinesString(ListWineView.winesCopy,etLand.getText().toString());
                  }
                  if(!etTaste.equals(null)){
                      ListWineView.filterWinesString(ListWineView.winesCopy,etTaste.getText().toString());
                  }
                  if(!etYear.equals(null)){
                      ListWineView.filterWinesString(ListWineView.winesCopy,etYear.getText().toString());
                  }
                  if(cbBiologic.isChecked()){
                      ListWineView.filterWinesString(ListWineView.winesCopy, "true");
                  }else{
                      ListWineView.filterWinesString(ListWineView.winesCopy, "false");
                  }
                  if(cbMouserend.isChecked()){
                      ListWineView.filterWinesString(ListWineView.winesCopy, "true");
                  }else{
                      ListWineView.filterWinesString(ListWineView.winesCopy, "false");
                  }
                  if(spinner1.getSelectedItemId()!=0){
                      ListWineView.filterWinesString(ListWineView.winesCopy,spinner1.getSelectedItem().toString());
                  }


                  ListWinesViewFilterActivity.this.finish();
              }
        });

      }

我想我应该在这里使用 notifyDatasetChange() :

btnFilter.setText("Filter");
    btnFilter.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(ListWineView.this, ListWinesViewFilterActivity.class);
            startActivity(i);
            adapter.notifyDataSetChanged();
        }
    });

但列表视图并不令人耳目一新。

编辑我现在已经更改了一些代码,并让它工作。

解决方案作为答案发布。

4

2 回答 2

1

尝试 startActivityForResult() 而不是 startActivity() 和 onActivityResult() 如果控件是从对话框活动返回的,则调用 adapter.notifyDataSetChanged()

于 2013-11-05T11:25:18.113 回答
0

放置过滤器方法的活动

 public static List<Wine> filterByCountry(String country) {
            if (country.equals("")) {
                return wines;
            }
            ArrayList<Wine> winesFil = new ArrayList<Wine>(wines);
            for(Wine wine : winesFil) {
                if (!wine.getCountry().contains(country)) {
                    filteredList.remove(wine);
                }
            }
            return filteredList;
        }
        public static List<Wine> filterByTaste(String taste) {
            if (taste.equals("")) {
                return wines;
            }
            ArrayList<Wine> winesFil = new ArrayList<Wine>(wines);
            for(Wine wine : winesFil) {
                if (!wine.getTaste().contains(taste)) {
                    filteredList.remove(wine);
                }
            }
            return filteredList;
        }
        public static List<Wine> filterByYear(int year ) {
            if (Integer.valueOf(year)==null) {
                return wines;
            }
            ArrayList<Wine> winesFil = new ArrayList<Wine>(wines);
            for(Wine wine : winesFil) {
                if (wine.getYear()!=year) {
                    filteredList.remove(wine);
                }
            }
            return filteredList;
        }

        public static List<Wine> filterByBiologic(boolean biologic) {
            if (!biologic) {
                return wines;
            }
            ArrayList<Wine> winesFil = new ArrayList<Wine>(wines);
            for(Wine wine : winesFil) {
                if (wine.getBiologic()!=biologic) {
                    filteredList.remove(wine);
                }
            }
            return filteredList;
        }

        public static List<Wine> filterBySparkling(boolean sparkling) {
            if (!sparkling) {
                return wines;
            }
            ArrayList<Wine> winesFil = new ArrayList<Wine>(wines);
            for(Wine wine : winesFil) {
                if (wine.getSparkling()!=sparkling) {
                    filteredList.remove(wine);
                }
            }
            return filteredList;
        }

        public static List<Wine> filterByColor(String color) {
            if (color.equals("all")) {
                return wines;
            }
            ArrayList<Wine> winesFil = new ArrayList<Wine>(wines);
            for(Wine wine : winesFil) {
                if (!wine.getCountry().contains(color)) {
                    filteredList.remove(wine);
                }
            }
            return filteredList;
        }

        public static void clearFilter()
        {
            adapter.clear();
            for(Wine wine : winesCopy)
            {
            adapter.add(wine);
            }
            adapter.notifyDataSetChanged();
            lv.setAdapter(adapter);
            filteredList = new ArrayList<Wine>(wines);
        }

        public static void setFilter()
        {
            adapter.clear();
            for(Wine wine : filteredList)
            {
            adapter.add(wine);
            }
            adapter.notifyDataSetChanged();
            lv.setAdapter(adapter);
        }

给出过滤输入的活动:

 @Override
              public void onClick(View v) {
                    ListWineView.clearFilter();
                    ListWineView.filterByCountry(etLand.getText().toString());
                    if(!etYear.getText().toString().equals("")){
                    ListWineView.filterByYear(Integer.valueOf(etYear.getText().toString()));
                    }
                    ListWineView.filterByTaste(etTaste.getText().toString());
                    ListWineView.filterByBiologic(cbBiologic.isSelected());
                    ListWineView.filterBySparkling(cbMouserend.isSelected());
                    ListWineView.filterByColor(spinner1.getSelectedItem().toString());
                    ListWineView.setFilter();
                    ListWinesViewFilterActivity.this.finish();
              }
        });
于 2013-11-06T08:19:29.283 回答