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
| public class TesteurAdapter extends ArrayAdapter<Testeur>{
List<Testeur> testeur;
LayoutInflater inflater;
public TesteurAdapter(Context context, List<Testeur> testeur)
{
super(context, R.layout.custom_listview_testeur, R.id.nom, testeur);
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Planet to display
Testeur testeur = (Testeur) this.getItem(position);
// The child views in each row.
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null)
{
convertView = inflater.inflate(R.layout.custom_listview_testeur, null);
// Find the child views.
textView = (TextView) convertView
.findViewById(R.id.nom);
checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
// Optimization: Tag the row with it's child views, so we don't
// have to
// call findViewById() later when we reuse the row.
convertView.setTag(new TesteurViewHolder(textView, checkBox));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
Testeur testeur = (Testeur) cb.getTag();
testeur.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else
{
// Because we use a ViewHolder, we avoid having to call
// findViewById().
TesteurViewHolder viewHolder = (TesteurViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag(testeur);
// Display planet data
checkBox.setChecked(testeur.isChecked());
textView.setText(testeur.getNom());
return convertView;
}
public Object onRetainNonConfigurationInstance()
{
return testeur;
}
} |
Partager