我在 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();
}
});
但列表视图并不令人耳目一新。
编辑我现在已经更改了一些代码,并让它工作。
解决方案作为答案发布。