我正在尝试在gridview中实现搜索栏。所以我试图在customfilter中使用notifydatasetchanged(),但它说方法无法解决。这是代码。提前提前。
package com.example.krishna.pokemongopokemons;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by krishna on 7/15/2016.
*/
public class ImageAdapter extends BaseAdapter implements Filterable {
private Context mContext;
CustomFilter filter;
String[] x;
public ImageAdapter(Context c,String[] x) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder mVHolder;
if (convertView == null) {
// if it's not recycled, initialize some attributes
convertView = inflater.inflate(R.layout.grid_single, parent, false);
mVHolder=new ViewHolder();
mVHolder.mImageView=(ImageView)convertView.findViewById(R.id.grid_image);
//mVHolder.mTextView=(TextView)convertView.findViewById(R.id.text);
convertView.setTag(mVHolder);
/*grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);*/
//imageView.setLayoutParams(new GridView.LayoutParams(3,3));
//imageView.setAdjustViewBounds(true);
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setPadding(8, 8, 8, 8);
} else {
mVHolder=(ViewHolder)convertView.getTag();
//grid = (View) convertView;
}
//TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
imageView.setImageResource(mThumbIds[position]);
mVHolder.mImageView.setImageResource(mThumbIds[position]);
mVHolder.mTextView.setText(web[position]);
textView.setText(web[position]);
return convertView;}
// references to our images
private Integer[] mThumbIds = {..
..};
//references to textview
String[] web = {
......
} ;
@Override
public Filter getFilter() {
if(filter==null){
filter=new CustomFilter();
}
return filter;
}
}
class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results=new FilterResults();
arrays a=new arrays();
int[] array_list=new int[150];
if(charSequence!=null & charSequence.length()>0){
charSequence=charSequence.toString().toUpperCase();
for(int i=0;i<a.web.length;i++){
if(a.web[i].toUpperCase().contains(charSequence)){
for(int j=0;j<array_list.length;j++){
if(array_list[j]==0){
array_list[j]=i;
break;
}
}
}
}
results.count=array_list.length;
results.values=array_list;
}
else
{
results.count=a.web.length;
results.values=a.web;
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
int[] x= (int[]) filterResults.values;
notifyDataSetChanged();
}
}
class ViewHolder {
ImageView mImageView;
}
无法在customfilter类中使用notifydatasetchanged()。
class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results=new FilterResults();
arrays a=new arrays();
int[] array_list=new int[150];
if(charSequence!=null & charSequence.length()>0){
charSequence=charSequence.toString().toUpperCase();
for(int i=0;i<a.web.length;i++){
if(a.web[i].toUpperCase().contains(charSequence)){
for(int j=0;j<array_list.length;j++){
if(array_list[j]==0){
array_list[j]=i;
break;
}
}
}
}
results.count=array_list.length;
results.values=array_list;
}
else
{
results.count=a.web.length;
results.values=a.web;
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
int[] x= (int[]) filterResults.values;
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
您需要为CustomFilter类创建构造函数并将适配器传递到那里:
ImageAdapter myImageAdapter;
public CustomFilter(ImageAdapter imageAdapter) {
this.myImageAdapter = imageAdapter;
}
然后在你的publishResults方法中:
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
int[] x= (int[]) filterResults.values;
myImageAdapter.notifyDataSetChanged();
}