2016-07-20 13 views
-1

Ich versuche, searchbar in gridview.So implementieren, dass ich versuche, notifydatasetchanged() in customfilter verwenden, aber es sagt Methode kann nicht gelöst werden.Hier ist der code.Thankyou im Voraus.Fehler: Kann Methode nicht auflösen 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; 


} 

kann nicht notifydatasetchanged() in benutzerdefinierten Filterklasse verwenden.

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

    } 


} 
+1

Mögliches Duplikat [Kann nicht verwenden notifyDataSetChanged zu update adapter] (http://stackoverflow.com/questions/28326942/cannot-use-notifydataset-change-to-update-adapter) – Ironman

Antwort

0

Sie müssen Konstruktor Ihre Custom Klasse machen und den Adapter dort passieren:

ImageAdapter myImageAdapter; 
public CustomFilter(ImageAdapter imageAdapter) { 
this.myImageAdapter = imageAdapter; 
} 

Und dann in Ihrer publishResults Methode:

@Override 
protected void publishResults(CharSequence charSequence, FilterResults filterResults) { 
    int[] x= (int[]) filterResults.values; 
    myImageAdapter.notifyDataSetChanged();  

}