2016-05-14 3 views
0

Все в коде ниже работает, кроме тех случаев, когда вы пытаетесь найти значение, приложение просто не отвечает, когда вы вводите буквы в searchview, которые должны фильтровать gridview.Search View is not search my gridview

Cheers, 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

ответ

0

у вас есть немного ошибка в этой строке:

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

ошибка относится ; символ в конце if заявления. Он делает код ниже if каждый раз, поэтому фильтр не работает. Просто удалите ; символ после if заявление

+0

Я не могу вас поблагодарить, это заставило меня уйти от бонкеров после долгого работы! – Kripzy