I have the following custom adapter for a GridView
:
public class AdapterGrid extends BaseAdapter implements Filterable{
private Context context;
private int layout;
private List<String> items;
public AdapterGrid(Context contexto, int layout, List<String> items) {
this.context = contexto;
this.layout = layout;
this.items = items;
}
@Override
public int getCount() {
return this.items.size() ;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int pos, View convertView, ViewGroup viewGroup) {
/////PATRO VIEW HOLDER
View inflateView = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
inflateView = layoutInflater.inflate(R.layout.grid_stamp, null);
//// esto reemplaza el getItem
String currentName = items.get(pos);
TextView numberStamp = inflateView.findViewById(R.id.numberstamp);
numberStamp.setText(""+pos);
return inflateView;
}
@Override
public Filter getFilter() {
return null;
}
}
and here I believe it:
final AdapterGrid itemsAdapter = new AdapterGrid(this, R.layout.grid_stamp, nitems);
gridView.setAdapter(itemsAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
});
I need to implement a Filter
for that gridview
, that is to say that when writing in edittext
can be filtered, I have implemented the Filterable
, but I do not know how to filter it, thanks.
- UPDATED -
I did not know what could be done with the @YorchSircam method, and without implementing filterable I was able to solve it, I made a copy of Items, and created a method in which it searches for the text that the user sends, adding in it the ones I find and showing the copied list:
public void filtrar(String texto){
//aca implementas tu filtro a como gustes, ejemplo
for (String object: this.items) {
Log.v(" ♠ FILTRAR true |","TEXTO ***"+texto+ " CONTENTARRAY "+object);
}
itemsMostrados.clear();
int possearch;
for (String object: this.items) {
possearch=object.indexOf(texto);
if(possearch > -1){
itemsMostrados.add(object);
}
}
notifyDataSetChanged();
}
public View getView(int pos, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
/////PATRO VIEW HOLDER
if(convertView==null){
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
convertView = layoutInflater.inflate(R.layout.grid_stamp, null);
holder = new ViewHolder();
holder.numeroestampilla = convertView.findViewById(R.id.numberstamp);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//// esto reemplaza el getItem
String currentName = itemsMostrados.get(pos);
holder.numeroestampilla.setText(""+pos+" "+currentName);
return convertView;
}