Constructor <Classadapter> in <Classadapter> cannot be applied to certain types

Asked

Viewed 51 times

0

I’m trying to implement a Searchview next to Recyclerview to make filtering book records occur. I am having problems in the following file line ListDonos.java:

Row:

recyclerAdapter = new DonoAdapter();

The error of this line is:

error: constructor Donoadapter in class Donoadapter cannot be Applied to Given types; required: List<Dono>,Context found: no Arguments Reason: actual and formal argument lists differ in length.

  • Filing cabinet ListarDonos.java
public class ListarDonos extends AppCompatActivity {

    RecyclerView recyclerView;
    DonoAdapter recyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listar_donos);
        setTitle("Listar donos");

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        Conexao conexao = new Conexao(this);
        List<Dono> donos = conexao.ReadDonos();
        
        if (donos.size() > 0){
            DonoAdapter donoadapter = new DonoAdapter(donos,ListarDonos.this);
            recyclerView.setAdapter(donoadapter);
        } else {
            Toast.makeText(this, "Não existem donos no banco de dados.", Toast.LENGTH_SHORT).show();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.pesquisar_dono,menu);
        MenuItem menuItem = menu.findItem(R.id.buscar_dono);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                recyclerAdapter.getFilter().filter(newText);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
}
  • Filing cabinet DonoAdapter.java
public class DonoAdapter extends RecyclerView.Adapter<DonoAdapter.ViewHolder> implements Filterable {

    List<Dono> donos;
    List<Dono> donos2;
    Context context;
    Conexao conexao;

    public DonoAdapter(List<Dono> donos, Context context) {
        this.donos = donos;
        this.donos2 = new ArrayList<>(donos);
        this.context = context;
        conexao = new Conexao(context);
    }
    // Método para obter o filtro
    @Override
    public Filter getFilter() {
        return filter;
    }
    // Realiza a filtragem
    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            List<Dono> filtro_donos = new ArrayList<>();
            if (charSequence.toString().isEmpty()){
                filtro_donos.addAll(donos2);
            } else {
                for (Dono doninho: donos2){
                    if (doninho.getNome_dono().toLowerCase().contains(charSequence.toString().toLowerCase())){
                        filtro_donos.add(doninho);
                    }
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = filtro_donos;
            return filterResults;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults filterResults) {
            donos.clear();
            donos.addAll((Collection<? extends Dono>) filterResults.values);
            notifyDataSetChanged();
        }
    };
}
  • Your class DonoAdapter has only one constructor and it needs two arguments.

  • @Emersonpardo is this builder I got lost in.

  • You need to make the call like this: recyclerAdapter = new DonoAdapter(listaDonos, context);

  • listaDonos comes from where?

  • The code is yours ,at some point you need to create an instance of a List<Dono> to pass in the builder of DonoAdapter.

1 answer

0

Your class DonoAdapter stretches RecyclerView.Adapter but in the line that gives error:

recyclerView.getFilter().filter(newText); // ERROR: Cannot resolv method 'getFilter()

is called the getFilter of RecyclerView which, by the text of the error message, does not have the method getFilter().

Consulting to documentation of Recyclerview you can see that the class RecyclerView.Adapter does not have the filter method. You will need to cast to call DonoAdapter.filter() which is a method unique to your Adapter. Something like:

 DonoAdapter donoAdapter = (DonoAdapter)recyclerView.getAdapter();
 donoAdapter.filter(newText);
  • I didn’t understand, I tried to change here and it didn’t solve.

  • Tried to change this line recyclerView.getFilter().filter(newText); to look like this: recyclerView.getAdapter().getFilter().filter(newText);? Made the same mistake?

  • I switched to recyclerView.getAdapter().getFilter().filter(newText); and made the mistake Cannot resolv method 'getFilter()'

  • The class RecyclerView.Adapter has not the method filter. You will need to cast to call the filter. Something like DonoAdapter donoAdapter = (DonoAdapter)recyclerView.getAdapter(); and then call the filter: donoAdapter.filter(newText)

  • Can you rephrase your answer?

  • I edited to include the answer with the cast.

  • Now I understand, did not return any error, I will do the test here and I’ll be back to answer you.

  • Okay, if there is a new error and you think it has no relation to this one, mark this as solved so that others know what the solution was and open another question with the new error. If the error is still related to this, edit your question with the solution attempt and the new error.

  • I went to do the tests and the app always stops running when I go to the listing page

  • It didn’t work, Emerson.

  • Get more information about the error to update your question?

  • I can not say, the compiler does not accuse a serious error.

  • I returned the passage to what it was before.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.