如果我更改Spinner值,如何刷新新的Recyclerview数据?

时间:2017-07-25 06:26:50

标签: android android-recyclerview adapter

所以基本上我有2个带有一组值的Spinners。当我更改Spinner值时,Recyclerview将刷新并使用一组新数据进行更新。我被困在这里,欢迎任何帮助,提前谢谢!

private String[] vocSpinner;
private String[] popSpinner;
private List<Standard> standardList = new ArrayList<>();
private RecyclerView recyclerView;
private StandardsAdapter sAdapter;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Standard");

recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
sAdapter = new StandardsAdapter(standardList);
RecyclerView.LayoutManager mLayoutManager = new 
LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(sAdapter);

this.vocSpinner = new String[]{
            "CDO/Diver/Gds/Fitness Spec", "Combat/Service"
    };

    this.popSpinner = new String[]{
            "NSmen", "Regular/NSF", "Pre-enlistee"
    };

Spinner v = (Spinner) getActivity().findViewById(R.id.spinner_Voc);
final Spinner p = (Spinner) getActivity().findViewById(R.id.spinner_PopGp);

ArrayAdapter<String> adapterV = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, vocSpinner);
v.setAdapter(adapterV);

ArrayAdapter<String> adapterP = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, popSpinner);
p.setAdapter(adapterP);

v.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            switch (position) {
                case 0:
                    if (p.getSelectedItem().equals("NSmen")) {
                        addStandardToList("Gold", ">90pts");
                        addStandardToList("Silver", ">75pts");
                        // I need help here!!
                        sAdapter.notifyDataSetChanged();


                    } else if (p.getSelectedItem().equals("Regular/NSF")) {
                        addStandardToList("Gold", ">85pts");
                        sAdapter.notifyDataSetChanged();

                    } else if (p.getSelectedItem().equals("Pre-enlistee")) {                           
                    }
                    break;

                case 1:
                    if (p.getSelectedItem().equals("NSmen")) {
                    } else if (p.getSelectedItem().equals("Regular/NSF")) {
                    } else if (p.getSelectedItem().equals("Pre-enlistee")) {                       
                    }
                    break;
            }
        }

@Override
        public void onNothingSelected(AdapterView<?> parentView) {
        }
    });

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_standard, container, false);
    return view;
}

private void addStandardToList(String stdStan, String stdPts) {
    Standard standardObj = new Standard (stdStan, stdPts);
    standardList.add(standardObj);
    sAdapter.notifyDataSetChanged();
}

}

2 个答案:

答案 0 :(得分:0)

@Ryan你的问题在于逻辑而不是notifyAdapterDataSet。你所做的是用不同的项填充adapterV和adapterP,并且只设置adapterV的itemSelectedListener。现在,在您选择的侦听器中,您有一个if条件,if条件的值与您在adapterP中设置的值相关。所以他们永远不会在v.onitemselected中被选中因此问题。改变这一行

v.setOnItemSelectedListener

p.setOnItemSelectedListener

然后更改P的值以查看选择是否有效,然后修改逻辑

更新1: 要根据您的评论清除列表,只需在您选择的on itemselected方法中添加standardList.clear()作为第一行,如下所示 -

public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        standardList.clear();
      switch (position) {
            case 0:
                //Your code here remains as it is now.

答案 1 :(得分:0)

如果您需要清除回收站视图并添加新值。然后首先需要清除传递给适配器

的arraylist中的值
standardList.clear();

然后你必须通过调用

来添加新值
 addStandardToList("Gold", ">90pts");

然后,您必须通过调用

要求适配器刷新回收器视图上的数据
sAdapter.notifyDataSetChanged();