使用更多加载后无法在recyclerview上搜索过滤器

时间:2019-02-08 01:22:58

标签: android android-recyclerview

我有一个回收站视图,可以实现更多负载。但是当我添加搜索过滤器和排序过滤器时,由于空对象引用而导致了错误。这是logcat上的错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.ilham.obatherbal.herbalJava.herbalModel.getNama()' on a null object reference

我想添加基于名称的搜索过滤器,并基于某些内容进行排序。

在添加更多功能之前,搜索过滤器实际上已经工作。这是我主要活动的代码:

       public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView = inflater.inflate(R.layout.fragment_herbal, container, false);
            herbalModels = new ArrayList<>();

            RequestQueue queue = MySingleton.getInstance(this.getActivity().getApplicationContext()).getRequestQueue();
            get20Data();
            sortData(rootView);
            return rootView ;

        }
    private void StartRecyclerView(View rootView) {
            mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_herbal);
            mRecyclerView.setHasFixedSize(true);

            mLayoutManager = new LinearLayoutManager(getActivity());

            // use a linear layout manager
            mRecyclerView.setLayoutManager(mLayoutManager);

            // create an Object for Adapter
            mAdapter = new herbalAdapter(mRecyclerView,getActivity(),herbalModels);

            mRecyclerView.setAdapter(mAdapter);

            mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    //add null , so the adapter will check view_type and show progress bar at bottom
                    herbalModels.add(null);
                    mAdapter.notifyItemInserted(herbalModels.size() - 1);
                    handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //   remove progress item
                            herbalModels.remove(herbalModels.size() - 1);
                            mAdapter.notifyItemRemoved(herbalModels.size());
                            //add items one by one
                            int start = herbalModels.size();
                            int end = start + 20;
                            loadMore(start,end);

                           }
                    },5000);

                }
            });




        }
private void sortData(final View rootView) {

        final Spinner spinner = (Spinner) rootView.findViewById(R.id.filter_herbal);
// Create an ArrayAdapter using the string array and a default spinner layout
        List<categoriesHerbal> itemList = new ArrayList<categoriesHerbal>();
        itemList.add(
                new categoriesHerbal(
                        "1",
                        "All"
                )
        );itemList.add(
                new categoriesHerbal(
                        "2",
                        "Jamu"
                )
        );itemList.add(
                new categoriesHerbal(
                        "3",
                        "Kampo"
                )
        );
        ArrayAdapter<categoriesHerbal> spinnerAdapter = new ArrayAdapter<categoriesHerbal>(getActivity(), android.R.layout.simple_spinner_item, itemList);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears

// Apply the adapter to the spinner
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0)
                {
                    StartRecyclerView(rootView);
                    searchData(rootView);

                }
                else
                {
                    categoriesHerbal selectedValue = (categoriesHerbal) parent.getItemAtPosition(position);
                    String categories = (String)selectedValue.getCategories();
                    String idCategories = (String )selectedValue.getIdCategories();
                    Log.d(TAG,"selected"+categories+id);
                    categories(idCategories);
                    searchOnCategories(rootView,idCategories);

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
private void searchData(View rootView) {
        search = (EditText) rootView.findViewById(R.id.search_herbal);
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                filter(s.toString());
            }
        });

    }
 private void filter(String s) {
        ArrayList<herbalModel> filteredList = new ArrayList<>();
        for (herbalModel item : herbalModels){
            if (item.getNama().toLowerCase().contains(s.toLowerCase()))
            {
                filteredList.add(item);
            }
        }
        mAdapter.filterlist(filteredList);
    }

1 个答案:

答案 0 :(得分:0)

此错误表示该项目为空。在for循环上放置一个断点,然后查看HerbalModels数组中的内容。那应该指出为什么在此循环中有空引用。

private void filter(String s) {
        ArrayList<herbalModel> filteredList = new ArrayList<>();
        for (herbalModel item : herbalModels){
            if (item.getNama().toLowerCase().contains(s.toLowerCase()))
            {
                filteredList.add(item);
            }
        }
        mAdapter.filterlist(filteredList);
    }