Skip to content

Instantly share code, notes, and snippets.

@johnkil
Created June 24, 2014 14:34
Show Gist options
  • Save johnkil/ed7752258cf0ab74d081 to your computer and use it in GitHub Desktop.
Save johnkil/ed7752258cf0ab74d081 to your computer and use it in GitHub Desktop.
Implementation of ArrayAdapter based on the CursorAdapter.
/*
* Copyright 2014 Evgeny Shishkin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Adapter that exposes data from an array of objects to a {@link android.widget.ListView ListView}
* widget. Implementation is based on the {@link android.widget.CursorAdapter CursorAdapter}.
*
* @author Evgeny Shishkin
*/
public abstract class BaseArrayAdapter<T> extends BaseAdapter {
private final Context mContext;
private final LayoutInflater mLayoutInflater;
private List<T> mItems;
/**
* Constructor.
*
* @param context The context
*/
public BaseArrayAdapter(Context context) {
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
/**
* Constructor.
*
* @param context The context
* @param items The objects to represent in the ListView
*/
public BaseArrayAdapter(Context context, List<T> items) {
this(context);
mItems = items;
}
/**
* Set items to display.
*
* @param items The items
*/
public void setItems(List<T> items) {
if (items != mItems) {
mItems = items;
notifyDataSetChanged();
}
}
/**
* Return the items.
*
* @return The items
*/
public List<T> getItems() {
return mItems;
}
/**
* Returns the context associated with this array adapter.
*
* @return The context
*/
public Context getContext() {
return mContext;
}
/**
* {@inheritDoc}
*/
@Override
public int getCount() {
return mItems != null ? mItems.size() : 0;
}
/**
* {@inheritDoc}
*/
@Override
public T getItem(int position) {
return mItems != null ? mItems.get(position) : null;
}
/**
* {@inheritDoc}
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* {@inheritDoc}
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final T item = getItem(position);
View view;
if (convertView == null) {
view = newView(mLayoutInflater, item, parent);
} else {
view = convertView;
}
bindView(view, mContext, item);
return view;
}
/**
* {@inheritDoc}
*/
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final T item = getItem(position);
View view;
if (convertView == null) {
view = newDropDownView(mLayoutInflater, item, parent);
} else {
view = convertView;
}
bindView(view, mContext, item);
return view;
}
/**
* Makes a new view to hold the item's data.
*
* @param inflater The LayoutInflater object that can be used to inflate item view
* @param item The item
* @param parent The parent to which the new view is attached to
* @return the newly created view.
*/
public abstract View newView(LayoutInflater inflater, T item, ViewGroup parent);
/**
* Makes a new drop down view to hold the item's data.
*
* @param inflater The LayoutInflater object that can be used to inflate item view
* @param item The item
* @param parent The parent to which the new view is attached to
* @return the newly created view.
*/
public View newDropDownView(LayoutInflater inflater, T item, ViewGroup parent) {
return newView(inflater, item, parent);
}
/**
* Bind an existing view to the item's data.
*
* @param view Existing view, returned earlier by newView
* @param context Interface to application's global information
* @param item The item
*/
public abstract void bindView(View view, Context context, T item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment