Created
March 5, 2016 08:42
-
-
Save luckycreationsindia/61055849d67f6a572987 to your computer and use it in GitHub Desktop.
SparseArrayAdapter for Spinner and Listview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.Resources; | |
import android.support.annotation.IdRes; | |
import android.support.annotation.LayoutRes; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
import android.util.SparseArray; | |
import android.view.ContextThemeWrapper; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import android.widget.ThemedSpinnerAdapter; | |
/* | |
* @author PlexusSquare Software Solutions Pvt Ltd | |
* */ | |
public class SparseArrayAdapter<E> extends BaseAdapter implements ThemedSpinnerAdapter { | |
private Context mContext; | |
private final LayoutInflater mInflater; | |
/** | |
* Contains the list of objects that represent the data of this ArrayAdapter. | |
* The content of this list is referred to as "the array" in the documentation. | |
*/ | |
private SparseArray<E> mObjects; | |
/** | |
* The resource indicating what views to inflate to display the content of this | |
* array adapter. | |
*/ | |
private int mResource; | |
/** | |
* The resource indicating what views to inflate to display the content of this | |
* array adapter in a drop down widget. | |
*/ | |
private int mDropDownResource; | |
/** | |
* Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. | |
*/ | |
private LayoutInflater mDropDownInflater; | |
/** | |
* If the inflated resource is not a TextView, {@link #mFieldId} is used to find | |
* a TextView inside the inflated views hierarchy. This field must contain the | |
* identifier that matches the one defined in the resource file. | |
*/ | |
private int mFieldId = 0; | |
/*// A copy of the original mObjects array, initialized from and then used instead as soon as | |
// the mFilter ArrayFilter is used. mObjects will then only contain the filtered values. | |
private SparseArray<E> mOriginalValues; | |
private ArrayFilter mFilter;*/ | |
/** | |
* Constructor | |
* | |
* @param context The current context. | |
* @param resource The resource ID for a layout file containing a TextView to use when | |
* instantiating views. | |
* @param objects The objects to represent in the ListView. | |
*/ | |
public SparseArrayAdapter(Context context, @LayoutRes int resource, @NonNull SparseArray<E> objects) { | |
this(context, resource, 0, objects); | |
} | |
/** | |
* Constructor | |
* | |
* @param context The current context. | |
* @param resource The resource ID for a layout file containing a layout to use when | |
* instantiating views. | |
* @param textViewResourceId The id of the TextView within the layout resource to be populated | |
* @param objects The objects to represent in the ListView. | |
*/ | |
public SparseArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId, | |
@NonNull SparseArray<E> objects) { | |
mContext = context; | |
mInflater = LayoutInflater.from(context); | |
mResource = mDropDownResource = resource; | |
mObjects = objects; | |
mFieldId = textViewResourceId; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public View getView(int position, View convertView, ViewGroup parent) { | |
return createViewFromResource(mInflater, position, convertView, parent, mResource); | |
} | |
private View createViewFromResource(LayoutInflater inflater, int position, View convertView, | |
ViewGroup parent, int resource) { | |
View view; | |
TextView text; | |
if (convertView == null) { | |
view = inflater.inflate(resource, parent, false); | |
} else { | |
view = convertView; | |
} | |
try { | |
if (mFieldId == 0) { | |
// If no custom field is assigned, assume the whole resource is a TextView | |
text = (TextView) view; | |
} else { | |
// Otherwise, find the TextView field within the layout | |
text = (TextView) view.findViewById(mFieldId); | |
} | |
} catch (ClassCastException e) { | |
Log.e("SparseArrayAdapter", "You must supply a resource ID for a TextView"); | |
throw new IllegalStateException( | |
"SparseArrayAdapter requires the resource ID to be a TextView", e); | |
} | |
E item = getItem(position); | |
if (item instanceof CharSequence) { | |
text.setText((CharSequence) item); | |
} else { | |
text.setText(item.toString()); | |
} | |
return view; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
/*public Filter getFilter() { | |
if (mFilter == null) { | |
mFilter = new ArrayFilter(); | |
} | |
return mFilter; | |
}*/ | |
public void setData(SparseArray<E> data) { | |
mObjects = data; | |
} | |
@Override | |
public int getCount() { | |
return mObjects.size(); | |
} | |
@Override | |
public E getItem(int position) { | |
return mObjects.valueAt(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return mObjects.keyAt(position); | |
} | |
/** | |
* <p>Sets the layout resource to create the drop down views.</p> | |
* | |
* @param resource the layout resource defining the drop down views | |
* @see #getDropDownView(int, android.view.View, android.view.ViewGroup) | |
*/ | |
public void setDropDownViewResource(@LayoutRes int resource) { | |
this.mDropDownResource = resource; | |
} | |
/** | |
* Sets the {@link Resources.Theme} against which drop-down views are | |
* inflated. | |
* <p/> | |
* By default, drop-down views are inflated against the theme of the | |
* {@link Context} passed to the adapter's constructor. | |
* | |
* @param theme the theme against which to inflate drop-down views or | |
* {@code null} to use the theme from the adapter's context | |
* @see #getDropDownView(int, View, ViewGroup) | |
*/ | |
@Override | |
public void setDropDownViewTheme(Resources.Theme theme) { | |
if (theme == null) { | |
mDropDownInflater = null; | |
} else if (theme == mInflater.getContext().getTheme()) { | |
mDropDownInflater = mInflater; | |
} else { | |
final Context context = new ContextThemeWrapper(mContext, theme); | |
mDropDownInflater = LayoutInflater.from(context); | |
} | |
} | |
@Override | |
public Resources.Theme getDropDownViewTheme() { | |
return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme(); | |
} | |
@Override | |
public View getDropDownView(int position, View convertView, ViewGroup parent) { | |
final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater; | |
return createViewFromResource(inflater, position, convertView, parent, mDropDownResource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment