如何搜索和过滤片段

时间:2018-03-28 21:36:40

标签: java android android-studio android-fragments

我可以显示搜索栏,但无法搜索任何内容,当我在搜索栏中输入任何内容并按Enter键时,它不会过滤或提供结果,所有内容都保持原样,请告诉我我的代码有什么问题以及为什么它没有提出任何结果,提前谢谢。

public class MyJobsFragment extends Fragment {

    MyJobsAdapter adapter;
    RecyclerView recyclerView;


public MyJobsFragment() {

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    View view = inflater.inflate(R.layout.fragment_my_jobs, container, 
    false);

    ArrayList<MyJobsModel> data = new ArrayList<>();
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));
    data.add(new MyJobsModel("Android Developer", "Vee Live", ""));

    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
    adapter = new MyJobsAdapter(data, getContext());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
    itemAnimator.setAddDuration(1000);
    itemAnimator.setRemoveDuration(1000);
    recyclerView.setItemAnimator(itemAnimator);

    return view;
}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search_view, menu);
    MenuItem item = menu.findItem(R.id.menuSearch);
    final SearchView searchView = new SearchView(((Jobs) 
    getActivity()).getSupportActionBar().getThemedContext());
    MenuItemCompat.setShowAsAction(item, 
    MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | 
    MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
    MenuItemCompat.setActionView(item, searchView);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

您没有在onQueryTextSubmit

上过滤recyclerview的数据

提交文本后,您需要过滤ArrayList并刷新recyclerview的数据。

  searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            ArrayList<MyJobsModel> filteredData = new ArrayList<MyJobsModel>();

            for (MyJobsModel myJobsModel : data) {
                if (myJobsModel.getName().equals(query)) {

                    filteredData.add(myJobsModel);

                }

            }

  adapter = new MyJobsAdapter(data, getContext());
                    recyclerView.setAdapter(adapter);
            return true ;


        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });