Saboor880 9 Junior Poster

Hello! I have a grid view with the row model having a image view and text view in it. I set a custom adapter to populate the the grid view with images and texts successfully.
But now I want to use a search view to filter the grid view on the base of text in text views. For this I implemented Filterable interface .
When i enter the string in my search view nothing happens. I am pasting my code also. Please check it . All the code is in one class.

Please view the code in the inner classes ImageAdapter and CustomerFilter specially. I have searched on internet for this problem, but by problem not solved. Please guide me what mistake i am doing?

package com.example.hp430.tabsexample;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import java.util.ArrayList;
import java.util.zip.Inflater;

import static com.example.hp430.tabsexample.MainActivity.MYPREF;
import static com.example.hp430.tabsexample.MainActivity.gridname;

public class TabFragment1 extends android.support.v4.app.Fragment {
    private AdView adView;
    SharedPreferences sharedpreferences;

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup `container, Bundle savedInstanceState) {`
View rootview = inflater.inflate(R.layout.tab_fragment_1,container,false);
SearchView sv = rootview.findViewById(R.id.searchview1);
GridView gridview = (GridView)rootview.findViewById(R.id.pfgrid);

  gridview.setAdapter(new ImageAdapter(getActivity()));

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
           new ImageAdapter(getActivity()).getFilter().filter(newText);
            return false;
        }
    });

    adView = rootview.findViewById(R.id.ad_view 
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            sharedpreferences = getActivity().getSharedPreferences(MYPREF, Context.MODE_PRIVATE);
            String n = sharedpreferences.getString(gridname,"namekey");
           // Toast.makeText(getContext(),n,Toast.LENGTH_SHORT).show();
           Intent i = new Intent(getActivity(),WebViewActivity.class);
           i.putExtra("type",n);
            i.putExtra("position",position);
           startActivity(i);
        }
    });
    // Inflate the layout for this fragment
    return rootview;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public void onPause() {
    super.onPause();
    if (adView != null) {
        adView.resume();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (adView != null) {
        adView.resume();
    }
    }

    // Inner Class ImageAdapter  in class TabFragment1
public class ImageAdapter extends BaseAdapter implements Filterable{

    private Context mContext;
    // Constructor
    public ImageAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        TextView txt;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null ) {

            convertView = inflater.inflate(R.layout.row_grid, parent, false);

            }
        imageView = convertView.findViewById(R.id.mimageView);
        txt = convertView.findViewById(R.id.mtextView);

        ViewGroup.LayoutParams lp = imageView.getLayoutParams();
        lp.width = 170;
        lp.height = 170;
        imageView.requestLayout();

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);

        imageView.setImageResource(mThumbIds[position]);
        txt.setText(mThumbNames[position]);

        return convertView;
    }

    public Integer[] mThumbIds = {
            R.drawable.alkaram,R.drawable.khaadi,R.drawable.deepakperwani,R.drawable.hsy,
            R.drawable.chenone,R.drawable.mariab,R.drawable.warda,R.drawable.firdous,R.drawable.sanasafinaz,
            R.drawable.sapphire,R.drawable.kayseria,R.drawable.jj,R.drawable.bareeze,R.drawable.gulahmad,
            R.drawable.crossstich,R.drawable.nishatlinen
   };

    public String[] mThumbNames = {
            "Alkaram", "Khaadi","DeepakPerwani", "HSY","Chenone","Mari.b","Warda","Firdous","SanaSafinaz",
            "Sapphire","Kayseria","Junaid Jamshaid","Bareeze","Gul Ahmad","CrossStich","NishatLinen"

    };

    @Override
    public Filter getFilter() {
       Filter filter=null;
        if(filter==null){
            filter = new CustomFilter();
        }
        return filter;
    }

    //Inner class CustomeFilter  in class ImageAdapter
    class CustomFilter extends Filter{
        ArrayList Filterlist = new ArrayList();
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(constraint !=null && constraint.length()>0){

                constraint = constraint.toString().toUpperCase();

                //Filtering
                for(int i=0;i<mThumbNames.length;i++){
                    if(mThumbNames[i].toUpperCase().contains(constraint)){
                        int image = mThumbIds[i];
                        String name = mThumbNames[i];

                        Filterlist.add(name);

                    }
                }

                results.count = Filterlist.size();
                results.values =Filterlist;
                Toast.makeText(getActivity(),"count= "+Filterlist.size(),Toast.LENGTH_LONG).show();

            }

            else{
                results.count = Filterlist.size();
                results.values =Filterlist;
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Filterlist = (ArrayList ) results.values;
            notifyDataSetChanged();
        }
    }// end class CustomFilter

   }//end  class ImageAdapter

}// end parent class i-e TabFragment1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.