SearchView没有过滤片段中的gridview

时间:2018-06-23 13:05:49

标签: android

我有一个网格视图,其中的行模型具有图像视图和文本视图。我设置了一个自定义适配器,以成功用图像和文本填充网格视图。但现在我想使用搜索视图来过滤网格视图 文本视图中的文本基础。为此,我实现了Filterable接口。当我在搜索视图中输入字符串时,没有任何反应。我也在粘贴我的代码。请检查一下 。所有代码都在一个类中。

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

请特别查看内部类ImageAdapter和CustomerFilter中的代码。我已经在互联网上搜索了此问题,但问题仍未解决。请指导我我在做什么错误?

1 个答案:

答案 0 :(得分:0)

是的,是对的MikeM。我每次都创建一个新的ImageAdapter实例。我将final添加到Gridview的开头,然后将onQueryTextChange()中的行更改为((ImageAdapter)gridview.getAdapter())。getFilter()。filter(newText); 现在一切就绪。