Android:修复微调器选择的RecyclerView过滤?

时间:2017-08-04 03:11:47

标签: android android-recyclerview android-spinner

我有一个包含许多CardView的RecyclerView列表。当用户从微调器下拉对话框中选择一个项目时,我想过滤列表。微调器中的项目选择将列表缩小到较小的原始CardView集合。我的问题是当我向列表中添加一个新的CardView时,以某种方式过滤列表,该列表在用户使用微调器进行任何项目选择之前发生。因此,在Recyclerview列表中没有新的CardView显示,它只是一个空白的RecyclerView列表。我只想在用户从微调器中选择项目时过滤CardViews,而不是每次将CardView添加到RecyclerView列表时。我在这里缺少什么?

MainActivity
public class MainActivity extends AppCompatActivity {

    private RecyclerView sRecyclerView;    
    private MyRecylerAdapter adapter;
    private List<ListItem> allList;
    ...
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    // For filtering by the CardView type.
    final Spinner spinner2;        
    spinner2 = (Spinner) findViewById(R.id.skycard_filter);
    String[] spinnerItems2 = getResources().getStringArray(R.array.skycard_filters);        
    final CustomAdapter<String> adapter2 = new CustomAdapter<>
            (this,android.R.layout.simple_spinner_item, spinnerItems2);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);
    spinner2.setPrompt("Filter CardView type");

    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,
            int position, long id) {

            String type = spinnerItems2[position];
            final ArrayList<ListItem> filteredModelList2 = adapter.filter2(adapter.listItems,type);
            adapter.setFilter(filteredModelList2);
        }
        @Override
        public void onNothingSelected (AdapterView < ? > adapterView){
            // auto-generated
        }
    });
}

Adapter
public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<ListItem> listItems;
    ...
    // Do Search filtering from MainActivity.
    public ArrayList<ListItem> filter2(List<ListItem> models, String filterType) {

        final ArrayList<ListItem> filteredModelList2 = new ArrayList<>();
        for (ListItem model:models) {
            final String cardTypeTest = model.getType();

            if (cardTypeTest.equals(filterType)) {
                filteredModelList2.add(model);
            }
        }
        return filteredModelList2;
    }

    public void setFilter2(ArrayList<ListItem> listItem) {
        listItems = new ArrayList<>();
        listItems.addAll(listItem);
        notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您总是使用“类型”一词过滤,您应该根据您从微调器中选择的单词进行过滤。

filterType

中还有一个参数filter2
public ArrayList<ListItem> filter2(List<ListItem> models, String filterType) {

        final ArrayList<ListItem> filteredModelList = new ArrayList<>();
        for (ListItem model:models) {
            final String cardTypeTest = model.getType();

            if (cardTypeTest.equalsIgnoreCase(filterType)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }

在打电话的时候你可以这样打电话,

        final String[] spinnerItems2 = getResources().getStringArray(R.array.skycard_filters);

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view,
                                       int position, long id) {

                String type = spinnerItems2[position];

                final ArrayList<ListItem> filteredModelList2 = adapter.filter2(adapter.listItems, type);

                adapter.setFilter(filteredModelList2);
//                switch (position) {
//                    case 0:
//                        
//                        break;
//                ...
//                    case 4:
//                        break;
//                }
            }
            @Override
            public void onNothingSelected (AdapterView < ? > adapterView){
                // auto-generated
            }
        });