1
Good morning guys, I would need to ask a question. I have an application where I have a ListView
that pulls the information from the database, and would like to make a search field in which I am using a EditText
, that’s above that ListView
, I’m trying to filter the information from ListView
by the description of the product and would like that when placed in the EditText
he brought the product information, but I’m not getting it. Someone knows some way to do this using the Cursor
and Adapter
? I’ve seen it in a lot of places, but it hasn’t worked yet.
List Products Code in Listview:
public void lprod(SQLiteDatabase db){
ArrayList<modelListprod> prod = new ArrayList<modelListprod>();
//Passa para dentro do while
//listprod = new modelListprod();
String descricao = "";
String ean = "";
String status = "";
Double precoprod;
String categoria;
int codigo;
final SQLiteDatabase d5 = dadosOpenHelper.getReadableDatabase();
String rawQuery = "SELECT produto.descricao, produto.ean, produto.status, produto.precoprod, categoria.descricao, produto.cod FROM produto INNER JOIN categoria ON categoria.id = produto.codigocateg";
Cursor cursor5 = d5.rawQuery(
rawQuery,
null
);
if (cursor5 != null) {
itensprod.notifyDataSetChanged();
if (cursor5.moveToFirst()) {
do {
//uma nova instância por cada registo
listprod = new modelListprod();
descricao = cursor5.getString(0);
ean = cursor5.getString(1);
status = cursor5.getString(2);
precoprod = cursor5.getDouble(3);
categoria = cursor5.getString(4);
codigo = cursor5.getInt(5);
listprod.setDescricao(descricao);
listprod.setCategoria(categoria);
listprod.setEan(ean);
listprod.setPreco(precoprod);
listprod.setStatus(status);
listprod.setCod(codigo);
//Adiciona ao array
listadeprod.add(listprod);
itensprod.notifyDataSetChanged();
} while (cursor5.moveToNext());
}
}
//
//Passa para dentro do while
//listadeprod.add(listprod);
AdapterProd adapterProd = new AdapterProd(this, listadeprod);
listproduto.setAdapter(adapterProd);
}
Adapter of the Listview:
class AdapterProd extends ArrayAdapter<modelListprod> implements Filterable {
MainActivity c = new MainActivity();
private Context context;
LayoutInflater layoutInflater;
CustomFilter filter;
private ArrayList<modelListprod> list;;
private ArrayList<modelListprod> lista = null;
private int valida = 1;
public AdapterProd(Context context, ArrayList<modelListprod> listap){
super(context, 0, listap);
this.list = listap;
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.lista = listap;
//this.lista.addAll(listap);
}
public class ViewHolder{
TextView descricao;
TextView categoria;
TextView preco;
TextView ean;
TextView status;
}
@Override
public int getCount(){
return list.size();
}
@Override
public modelListprod getItem(int position){
return list.get(position);
}
@Override
public long getItemId(int position){
return list.indexOf(getItem(position));
}
@Override
public View getView(final int position, View convertVieww, ViewGroup parent) {
final ViewHolder holder;
if (convertVieww == null){
holder = new ViewHolder();
convertVieww = layoutInflater.inflate(R.layout.list_prod, null);
holder.descricao = (TextView)convertVieww.findViewById(R.id.tvdescricao2);
holder.categoria = (TextView)convertVieww.findViewById(R.id.tvcategoria2);
holder.preco = (TextView)convertVieww.findViewById(R.id.tvpreco2);
holder.ean = (TextView)convertVieww.findViewById(R.id.tvean2);
holder.status = (TextView)convertVieww.findViewById(R.id.tvstatus2);
convertVieww.setTag(holder);
}else{
holder = (ViewHolder)convertVieww.getTag();
}
holder.descricao.setText(list.get(position).getDescricao());
holder.categoria.setText(list.get(position).getCategoria());
holder.preco.setText(list.get(position).getPreco().toString());
holder.ean.setText(list.get(position).getEan());
holder.status.setText(list.get(position).getStatus());
return convertVieww;
}
@Override
public Filter getFilter(){
if (filter == null){
filter= new CustomFilter();
}
return filter;
}
class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint != null && constraint.length()>0){
constraint=constraint.toString().toUpperCase();
ArrayList<modelListprod> filters = new ArrayList<modelListprod>();
for (int i=0; i<lista.size(); i++){
if (lista.get(i).getDescricao().toUpperCase().contains(constraint)){
modelListprod modelListprod = new modelListprod(lista.get(i).getDescricao());
filters.add(modelListprod);
}
}
results.count=filters.size();
results.values=filters;
}else{
results.count=lista.size();
results.values=lista;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
list=(ArrayList<modelListprod>) results.values;
notifyDataSetChanged();
}
}
}
Model of the Listview:
public class modelListprod implements Serializable {
int cod;
String descricao;
String categoria;
Double Preco;
String ean;
String status;
public modelListprod(String descricao) {
this.descricao = descricao;
}
public modelListprod() {
}
public int getCod() {
return cod;
}
public void setCod(int cod) {
this.cod = cod;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public String getCategoria() {
return categoria;
}
public void setCategoria(String categoria) {
this.categoria = categoria;
}
public Double getPreco() {
return Preco;
}
public void setPreco(Double preco) {
Preco = preco;
}
public String getEan() {
return ean;
}
public void setEan(String ean) {
this.ean = ean;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Could someone give me a hint as to how I should implement the filter that is on Adaptador
in the class that calls the ListView
?
Where is your Edittext? and what is the problem that is going like this that is there?
– Murillo Comino
Edittext is declared at the top of my Main Activity, in case I am not knowing how to use it to filter listview information. I tried a few ways but it didn’t work.
– Dev
If you can edit the question and add what you tried to do in Edittext.
– Murillo Comino
I managed to solve the problem now, I will add here the code working. Thank you for the post.
– Dev
Blz, sorry to ask the part of Edittext, but it was necessary for better understanding.
– Murillo Comino
Magina Murilo, I appreciate the participation, I really forgot to put it at the beginning, but thanks for the attention.
– Dev