Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.gawst.asyncdb.adapter;
  
  import java.util.List;
  
  import  android.content.Context;
  import  android.support.annotation.LayoutRes;
  import  android.support.annotation.NonNull;
  import  android.support.annotation.Nullable;
  import  android.view.LayoutInflater;
 import  android.view.View;
 import  android.view.ViewGroup;
 import  android.widget.BaseAdapter;
 import  android.widget.TextView;

A ListAdapter that only shows a subset of elements using the org.gawst.asyncdb.adapter.InMemoryFilteredAdapter.InMemoryFilter

Author(s):
Created by robUx4 on 08/01/2015.
See also:
org.gawst.asyncdb.adapter.InMemoryFilteredListAdapter
org.gawst.asyncdb.adapter.InMemoryFilteredTreeAdapter
 
 public class InMemoryFilteredAdapter<E> extends BaseAdapter {
 
 	protected final LayoutInflater mInflater;
 	@LayoutRes
 	protected final int layoutId;
 	@Nullable
 	protected final InMemoryFilter<E> filter;
 	@NonNull
 	private List<E> mData;
 	private UIHandler uiHandler;
 
 	public InMemoryFilteredAdapter(@NonNull Context context, @NonNull List<E> initialData, @LayoutRes int layoutResourceId, @Nullable InMemoryFilter<E> filter) {
 		this. = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 		this. = layoutResourceId;
 		this. = filter;
 		 = initialData;
 	}
 
 	public int getCount() {
 		return .size();
 	}
 
 	public E getItem(int position) {
 		return .get(position);
 	}
 
 	public boolean hasStableIds() {
 		return true;
 	}
 
 	public long getItemId(int position) {
 		if (position<.size())
 			return .get(position).hashCode();
 		return -1;
 	}
 
 	public View getView(int position, View convertView, ViewGroup parent) {
 		if (convertView==null)
 			convertView = .inflate(parentfalse);
 
 		TextView vt = (TextView) convertView.findViewById(android.R.id.text1);
 		vt.setText(.get(position).toString());
 
 		return convertView;
 	}

Helper function to filter the data from the org.gawst.asyncdb.adapter.InMemoryFilteredAdapter.InMemoryFilter.InMemoryFilterable source

Parameters:
source The data source.
filter The filter to apply to the source. May be null.
<E> Type of elements in the source.
Returns:
A filtered read-only List of elements coming from the source.
 
 	protected static <E> List<E> getFilteredData(@NonNull InMemoryFilter.InMemoryFilterable<E> source, @Nullable InMemoryFilter<E> filter) {
 		if (null == filter)
 			return source.getListCopy();
 		else
 			return filter.getFilteredData(source.getListCopy());
 	}
 
 	protected void setFilteredData(final List<E> filteredData) {
 		Runnable runner = new Runnable() {
 			public void run() {
 				 = filteredData;
 			}
 		};
 
 		if (null==)
 			 = new UIHandler();
 	}

Interface to filter the whole data stored in an org.gawst.asyncdb.AsynchronousDbHelper
	public static interface InMemoryFilter<E> {

Describes a data source that can produce a java.util.List object from its data.

Parameters:
<E> Type of elements in the generated java.util.List.
		public interface InMemoryFilterable<E> {

Returns:
A read-only java.util.List version of the source data.
		}

Filter data coming from a InMemoryFilterable source

Parameters:
data List of elements to filter.
Returns:
Filtered List of elements.
		@NonNull
		List<E> getFilteredData(List<E> data);
	}
New to GrepCode? Check out our FAQ X