无法使用Spinner过滤RecyclerView

时间:2016-09-27 11:54:08

标签: android string arraylist spinner startswith

我是Android开发人员,我在这里遇到一种尴尬的情况:我用静态值填充了一个微调器,我正在玩微调器中的项目以过滤由静态组成的字符串列表价值观(实际上是来自AZ的国家列表)。问题在于过滤本身。每当我在我的列表中测试一个国家的发生时,我就会继续虚假归还。这是代码:

private RecyclerView mRecyclerView;
private Spinner mSpinner;
private List<String> countries = new ArrayList<>();
private TestCountriesAdapter adapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_country, container, false);

    countries = Arrays.asList(getActivity().getResources().getStringArray(R.array.countries_array));
// This List is Not Empty !!.. I already cross-checked over and over
    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView)view.findViewById(R.id.recyler_view);
    mSpinner = (Spinner)view.findViewById(R.id.spinner_projects);

    initRecyclerView();

    mSpinner.setOnItemSelectedListener(this);
}

private void initRecyclerView()
{
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new TestCountriesAdapter(getActivity(), countries);
    mRecyclerView.setAdapter(adapter);
}

问题在于:

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {



Toast.makeText(getActivity(), "Size of List = " + countries.size(), Toast.LENGTH_SHORT).show();

 // Here again the List is Non-empty

    List<String> filteredList = new ArrayList<>();

    switch (position)
    {
        case 1:
            for (String country: countries)
            {
                if (country.startsWith("A")
                        || country.startsWith("B")
                        || country.startsWith("C")
                        || country.startsWith("D")
                        || country.startsWith("E")
                        )
                    filteredList.add(country);

            }

            break;
        case 2:
            for (String country: countries)
            {
                if (country.startsWith("F")
                        || country.startsWith("G")
                        || country.startsWith("H")
                        || country.startsWith("I")
                        || country.startsWith("j")
                        )
                    filteredList.add(country);
            }
            break;
        case 3:
            for (String country: countries)
            {
                if (country.startsWith("K")
                        || country.startsWith("L")
                        || country.startsWith("M")
                        || country.startsWith("N")
                        || country.startsWith("O")
                        )
                    filteredList.add(country);
            }

           break;

        default:
            break;
    }

    countries = filteredList;
    adapter.notifyDataSetChanged();


}

在每个“案例”中,每当我执行country.startsWith(“一个字母”)时,我都会返回false。 我真的不知道我继续这个“假”。 我做错了什么?......我的错是什么?

0 个答案:

没有答案