How to insert a list of records into an adapter for filtering functionality in Android Studio?

Asked

Viewed 12 times

0

I am having a certain difficulty in solving a listing problem via an adapter in the file ListarDonos.java.

The problem I mention is in the following line of the same file:

recyclerAdapter = new DonoAdapter(lista_donos);

The problem appears on this line in the form of the following message:

error: constructor DonoAdapter in class DonoAdapter cannot be applied to given types;
required: List<Dono>,Context
found: List<Dono>
reason: actual and formal argument lists differ in length

The probable error is on the line lista_donos = new ArrayList<>(); of the same file where I need to enter the records that exist in the app listing on ArrayList<>, but I don’t know how to solve.

  • ListaDonos.java
public class ListarDonos extends AppCompatActivity {

    RecyclerView recyclerView;
    DonoAdapter recyclerAdapter;
    List<Dono> lista_donos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listar_donos);
        setTitle("Listar donos");
        
        lista_donos = new ArrayList<>();
        recyclerView = findViewById(R.id.recyclerView);
        recyclerAdapter = new DonoAdapter(lista_donos);
        recyclerView.setAdapter(recyclerAdapter);
        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();
        }
    }

      // Trecho de campo de pesquisa de donos
      @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);
      }
}
  • 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 dono: donos2){
                    if (dono.getNome_dono().toLowerCase().contains(charSequence.toString().toLowerCase())){
                        filtro_donos.add(dono);
                    }
                }
            }
            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();
        }
    };
}

1 answer

0

The builder of the class DonoAdapter is defined as

public DonoAdapter(List<Dono> donos, Context context) {
    this.donos = donos;
    this.donos2 = new ArrayList<>(donos);
    this.context = context;
    conexao = new Conexao(context);
}

You’re building the class DonoAdapter as follows:

recyclerAdapter = new DonoAdapter(lista_donos);

Therefore, a parameter is missing in the constructor of DonoAdapter. To fix this, just pass the context of ListarDonos to the DonoAdapter:

recyclerAdapter = new DonoAdapter(lista_donos, this);

Browser other questions tagged

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