Implement Filterable to a Custom BaseAdapter - Android

0

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;

    }
    
asked by Andress Blend 27.03.2018 в 20:44
source

1 answer

2

I have not used filterable, but I propose something that may be useful for you. You can implement a public method on your adapter, and as you type, you send the string to that method to filter and update the list. You can have 2 lists in this case, a list with all the original items, and another list of items that you will display.

public class AdapterGrid extends BaseAdapter{
        private List<String> items, itemsMostrados;

        public AdapterGrid(..., List<String> items){
            this.items = items;
            itemsMostrados.addAll(items);
        }

        public void filtrar(String texto){
          //aca implementas tu filtro a como gustes, ejemplo
          itemsMostrados.clear();
          for(int i=0;i<items.size();i++){
              if(items.get(i).indexOf(texto)) 
                  itemsMostrados.add(items.get(i));
          }
          notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return this.itemsMostrados.size() ;
        }

And where is the input for your filter you can make use of it?

mFiltro.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            mAdapter.filter(charSequence.toString());
        }
    
answered by 27.03.2018 / 21:42
source