Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.kubatatami.judonetworking.observers;
  
  import  android.content.Context;
  import  android.view.View;
  import  android.view.ViewGroup;
  import  android.widget.ArrayAdapter;
  import  android.widget.Filter;
  
 import java.util.List;
 import java.util.Map;

Created with IntelliJ IDEA. User: jbogacki Date: 25.10.2013 Time: 10:15 To change this template use File | Settings | File Templates.
 
 public class ObserverAdapter<T> extends ArrayAdapter<T> {
 
     protected int[] resource;
     protected ObserverAdapterHelper adapterHelper;
     protected FilterInterface<T> filterInterface;
     protected Field  mObjectsField,mOriginalField;
 
     public ObserverAdapter(Context contextint... resource) {
         super(context, 0);
         this. = resource;
         init(context);
     }
 
     public ObserverAdapter(Context context, T[] objectsint... resource) {
         super(context, 0, objects);
         this. = resource;
         init(context);
     }
 
     public ObserverAdapter(Context contextList<T> objectsint... resource) {
         super(context, 0, objects);
         this. = resource;
         init(context);
     }
 
     public ObserverAdapter(Context contextMap<?, T> objectsint... resource) {
         super(context, 0, new ArrayList<T>(objects.values()));
         this. = resource;
         init(context);
     }
 
     protected void init(Context context) {
         try {
              = new ObserverAdapterHelper(context);
              = ArrayAdapter.class.getDeclaredField("mObjects");
             .setAccessible(true);
              = ArrayAdapter.class.getDeclaredField("mOriginalValues");
             .setAccessible(true);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         return .getView([getItemViewType(position)], getItem(position), convertViewparent);
     }
 
     public View getView(int position, View convertView, ViewGroup parentClass<?> holderClass) {
         return .getView([getItemViewType(position)], getItem(position), convertViewparentholderClass);
     }
 
     @Override
     public int getItemViewType(int position) {
         return position % getViewTypeCount();
     }
 
     @Override
     public int getViewTypeCount() {
         return .;
     }
 
     @Override
     public Filter getFilter() {
         return new Filter() {
             @Override
             protected FilterResults performFiltering(CharSequence constraint) {
                 FilterResults results = new FilterResults();
                 try {
                     List<T> mOriginals= (List<T>) .get(ObserverAdapter.this);
                     if(mOriginals==null){
                         mOriginals = (List<T>) .get(ObserverAdapter.this);
                         .set(ObserverAdapter.this,mOriginals);
                     }
                     // We implement here the filter logic
                     if (constraint == null || constraint.length() == 0) {
                         // No filter implemented we return all the list
                         results.values = mOriginals;
                         results.count = mOriginals.size();
                    } else {
                        List<T> newValues = new ArrayList<T>();
                        for (T value : mOriginals) {
                            if (.filter(constraintvalue)) {
                                newValues.add(value);
                            }
                        }
                        results.values = newValues;
                        results.count = newValues.size();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return results;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                try {
                    .set(ObserverAdapter.thisresults.values);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
    }
    public void setFilterInterface(FilterInterface<T> filterInterface) {
        this. = filterInterface;
    }
    public interface FilterInterface<T> {
        public boolean filter(CharSequence constraint, T item);
    }
New to GrepCode? Check out our FAQ X