搜索视图不搜索我的 gridview

Search View isnt searching my gridview

下面代码中的所有内容都有效,除了当您尝试搜索值时,当您在本应过滤网格视图的搜索视图中键入字母时,应用程序只是不响应。

干杯,Kripzy

主要Activity:

String[] Champions = {"Aatrox", "Ahri", "Akali"};
int[] Champimgs = {R.drawable.aatrox_square_0, R.drawable.ahri_square_0, R.drawable.akali_square_0};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gv = (GridView) findViewById(R.id.gridView);
    sv = (SearchView) findViewById(R.id.searchView);

    final Adapter adapter=new Adapter(this,this.getChampions());
    gv.setAdapter(adapter);

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

        @Override
        public boolean onQueryTextChange(String query) {

            adapter.getFilter().filter(query);

            return false;
        }
    });



}

private ArrayList<Champions> getChampions()
{
    ArrayList<Champions> champions = new ArrayList<Champions>();
    Champions p;

    for (int i = 0; i < Champions.length; i++)
    {
        p = new Champions(Champions[i], Champimgs[i]);
        champions.add(p);
    }
    return champions;
}

}

冠军

public Champions(String Champion, int Champimg) {

    this.Champimg=Champimg;
    this.Champion=Champion;}

    public String getChampion() {
        return Champion;
    }

    public int getChampimg() {
        return Champimg;
    }

    public void setChampion(String champion) {
    Champion = champion;
    }

    public void setChampimg(int champimg) {
    Champimg = champimg;
    }

}

适配器

public Adapter(Context ctx, ArrayList<Champions> Champion){
    this.c=ctx;
    this.Champion=Champion;
    this.filterlist=Champion;
}

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

@Override
public Object getItem(int position) {
    return Champion.get(position);
}

@Override
public long getItemId(int position) {
    return Champion.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.model, null);
    }
    TextView nameTxt = (TextView) convertView.findViewById(R.id.textView);
    ImageView img = (ImageView) convertView.findViewById(R.id.imageView);

    nameTxt.setText(Champion.get(position).getChampion());
    img.setImageResource(Champion.get(position).getChampimg());

    return convertView;
}

@Override
public Filter getFilter() {

    if (filter == null)
    {
        filter=new CustomFiler();
    }
    return filter;
}

class CustomFiler extends Filter
{
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults results=new FilterResults();

        if (constraint != null && constraint.length() > 0)
        {
            constraint=constraint.toString().toUpperCase();

            ArrayList<Champions> filters = new ArrayList<Champions>();

            for(int i = 0;i<filterlist.size();i++){

                if (filterlist.get(i).getChampion().toUpperCase().contains(constraint));
                {
                Champions p= new Champions (filterlist.get(i).getChampion(),filterlist.get(i).getChampimg());
                    filters.add(p);
            }
        }
            results.count=filters.size();
            results.values=filters;
        }else{
            results.count=filterlist.size();
            results.values=filterlist;
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        Champion = (ArrayList<Champions>) results.values;
        notifyDataSetChanged();
    }
}

}

你在这一行中有一个小错误:

if (filterlist.get(i).getChampion().toUpperCase().contains(constraint));

错误是 if 语句末尾的 ; 符号。它每次都使代码低于 if 运行,因此过滤器不起作用。只需在 if 语句

之后删除 ; 符号