1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
package com.android2ee.formation.mai.mmxiii.premiertp;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
* @author Mathias Seguy (Android2EE)
* @goals
* This class aims to:
* <ul>
* <li></li>
* </ul>
*/
public class HumanAdapter extends ArrayAdapter<Human> implements HumanAdapterCallBack {
LayoutInflater inflater;
HumanAdapterCallBack callBack;
/**
* @param context
* @param resource
* @param textViewResourceId
* @param objects
*/
public HumanAdapter(Context context, List<Human> objects) {
super(context, R.layout.item, objects);
inflater = LayoutInflater.from(getContext());
callBack=(HumanAdapterCallBack)context;
}
//Avoid using temp variable as method's variable
private static Human hum;
private static View myview;
private static ViewHolder viewHolder;
/*
* (non-Javadoc)
*
* @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
hum = getItem(position);
myview = convertView;
if (null == myview) {
if(getItemViewType(position)==0) {
myview = inflater.inflate(R.layout.item, null);
}else {
myview = inflater.inflate(R.layout.item_odd, null);
}
viewHolder=new ViewHolder(myview);
myview.setTag(viewHolder);
}else {
viewHolder=(ViewHolder) myview.getTag();
}
viewHolder.getTxvMessage().setText(hum.getMessage());
viewHolder.getTxvName().setText(hum.getName());
viewHolder.getTxvMessage().setOnClickListener(new MyOnClickListener(position, this));
viewHolder.getTxvName().setOnClickListener(new MyOnClickListener(position, this));
return myview;
}
/**
* @param position
*/
public void itemSelected(int position) {
callBack.itemSelected(position);
}
/******************************************************************************************/
/** Managing differents view **************************************************************************/
/******************************************************************************************/
/******************************************************************************************/
/** Managing the odd/even lines **************************************************************************/
/******************************************************************************************/
@Override
public int getViewTypeCount() {
// return the number of type managed by the list view:
// We have two types, one for the even line, the other for the odd lines
return 2;
}
@Override
public int getItemViewType(int position) {
// return the type of the element to be displayed at position position
// We have two types, one for the even line, the other for the odd lines
return position % 2;
}
public static class MyOnClickListener implements OnClickListener{
int position;
HumanAdapterCallBack hum;
/**
* @param position
*/
private MyOnClickListener(int position,HumanAdapterCallBack hum) {
super();
this.position = position;
this.hum=hum;
}
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
hum.itemSelected(position);
}
}
/******************************************************************************************/
/** ViewHolder **************************************************************************/
/******************************************************************************************/
public static class ViewHolder {
View boundView;
TextView txvName;
TextView txvMessage;
/**
* @param boundView
*/
private ViewHolder(View boundView) {
super();
this.boundView = boundView;
}
/**
* @return the txvName
*/
public final TextView getTxvName() {
if (null == txvName) {
txvName = (TextView) boundView.findViewById(R.id.nom);
}
return txvName;
}
/**
* @param txvName
* the txvName to set
*/
public final void setTxvName(TextView txvName) {
this.txvName = txvName;
}
/**
* @return the txvMessage
*/
public final TextView getTxvMessage() {
if (null == txvMessage) {
txvMessage = (TextView) boundView.findViewById(R.id.message);
}
return txvMessage;
}
/**
* @param txvMessage
* the txvMessage to set
*/
public final void setTxvMessage(TextView txvMessage) {
this.txvMessage = txvMessage;
}
}
} |
Partager