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.– Emerson Pardo
@Emersonpardo is this builder I got lost in.
– Erno
You need to make the call like this:
recyclerAdapter = new DonoAdapter(listaDonos, context);
– Emerson Pardo
listaDonos
comes from where?– Erno
The code is yours ,at some point you need to create an instance of a
List<Dono>
to pass in the builder ofDonoAdapter
.– Emerson Pardo