Home > OS >  adapter.getFilter().filter(newText) is not working in Activity but working properly in Fragment
adapter.getFilter().filter(newText) is not working in Activity but working properly in Fragment

Time:03-21

Fragment_Adapter_Calling.java file

MyStudentAdapterU adp = new MyStudentAdapterU(stuData, getContext());
stdRecyclerView.setAdapter(adp);
stdRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

 @Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {



    inflater.inflate(R.menu.search_menu,menu);

    MenuItem item=menu.findItem(R.id.search_menu);

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

        @Override
        public boolean onQueryTextChange(String newText) {

            adp.getFilter().filter(newText);

            return false;
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

but when I try to paste the same code in Activity.java file to call adapter, It shows an error "Cannot resolve method 'getFilter' in 'ProviderAdapter' on adapter.getFilter.filter(newText) line;

ProviderAdapter adp=new ProviderAdapter(ProviderActivityList.this,lists);
 r1.setAdapter(adp);
 r1.setLayoutManager(new LinearLayoutManager(ProviderActivityList.this));

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu,menu);

    MenuItem item=menu.findItem(R.id.search_menu);

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

        @Override
        public boolean onQueryTextChange(String newText) {

            adp.getFilter().filter(newText);


            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

Error picture Error Screenshot

CodePudding user response:

In the first code snippet, you set adp to MyStudentAdapterU which obviously has a getFilter() method.

In the second code snipper, you set adp to ProviderAdapter which obviously does NOT have a getFilter() method.

  • Related