搜索视图没有搜索我的gridview

时间:2016-05-14 11:18:42

标签: java android searchview

下面代码中的所有内容都有效,除了当您尝试搜索某个值时,当您在搜索视图中输入应该过滤gridview的字母时,应用程序才会响应。

干杯,Kripzy

主要活动:

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();
    }
}

}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

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

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

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

后删除if符号即可