Created
December 11, 2013 06:43
-
-
Save niusounds/7906005 to your computer and use it in GitHub Desktop.
Listを使う抽象ListAdapter
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 java.util.List; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
public abstract class AbstractListAdapter<T, V extends View> extends BaseAdapter { | |
private List<T> list = Collections.emptyList(); | |
@Override | |
public int getCount() { | |
return list.size(); | |
} | |
@Override | |
public T getItem(int position) { | |
return list.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
V view = convertView == null ? createView() : (V) convertView; | |
T item = getItem(position); | |
if (item != null) { | |
bind(view, item, position); | |
} | |
return view; | |
} | |
protected abstract V createView(); | |
protected abstract void bind(V view, T item, int position); | |
public List<T> getList() { | |
return list; | |
} | |
public void setList(List<T> list) { | |
this.list = list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment