ListViewのアダプタとHolderのパッケージ
MyAdapter.java
class MyAdapter extends BaseAdapter {
private ArrayList data;
public MyAdapter(List data){
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
// 1.
View view = View.inflate(context, R.layout.listviewitem, null);
// 2.
holder.textView = view.findViewById(R.id.textView1);
// 3. tag
view.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
// 4.
holder.textView.setText(" ");
return convertView;
}
}
static class ViewHolder {
TextView textView;
}
2パッケージBaseHolder
MyBaseHolder.java
public abstract class MyBaseHolder {
// item
private View mRootView;
// item
private T data;
// new , , , tag
public MyBaseHolder() {
mRootView = initView();
// 3. tag
mRootView.setTag(this);
}
// 1.
// 2. findViewById
public abstract View initView();
// item
public View getRootView() {
return mRootView;
}
// item
public void setData(T data) {
this.data = data;
refreshView(data);
}
// item
public T getData() {
return data;
}
// 4.
public abstract void refreshView(T data);
}
3エンティティHolder
SpecificHolder.java
public class SpecificHolder extends MyBaseHolder<String> {
private TextView mTextView;
@Override
public View initView() {
// 1.
View view = View.inflate(null, R.layout.item_listview, null);
// 2.
mTextView = (TextView) view.findViewById(R.id.textView1);
return view;
}
@Override
public void refreshView(String data) {
// 3.
mTextView.setText(data);
}
}
4パッケージBaseAdapter
MyBaseAdapter.java
public abstract class MyBaseAdapter extends BaseAdapter {
private ArrayList data;
public MyBaseAdapter(ArrayList data) {
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public T getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyBaseHolder baseHolder;
if(convertView == null){
// 1.
// 2. findViewById
// 3. tag
baseHolder = getHolder();
}else{
baseHolder = (MyBaseHolder) convertView.getTag();
}
// 4.
baseHolder.setData(getItem(position));
return baseHolder.getRootView();
}
public abstract MyBaseHolder getHolder();
}
5エンティティアダプタ
MyAdapter.java
class MyAdapter extends MyBaseAdapter<String>{
public MyAdapter(ArrayList data) {
super(data);
}
@Override
public MyBaseHolder getHolder() {
return new SpecificHolder();
}
}