-1
私はnotifydatasetchanged()をcustomfilterで使用しようとしているため、searchbarをgridview.Soに実装しようとしていますが、メソッドが解決できないと言われています。コードはあらかじめあります。エラー:メソッドnotifyDataSetChanged()を解決できません。 Android
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;
}
カスタムフィルタクラスでnotifydatasetchanged()を使用できません。
あなたのは、customFilterクラスにコンストラクタを作成し、そこにアダプターを渡す必要があり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();
}
}
[にnotifyDataSetChanged使用することはできませんの可能性の重複:あなたのpublishResults方法で
そしてをアップデートアダプタ](http://stackoverflow.com/questions/28326942/cannot-use-notifydatasetchanged-to-update-adapter) – Ironman