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
|
package com.examples;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CountryArrayAdapter extends ArrayAdapter<Country> {
private static final String tag = "CountryArrayAdapter";
private static final String ASSETS_DIR = "images/";
private Context context;
private ImageView countryIcon;
private TextView countryName;
private TextView countryAbbrev;
private List<Country> countries = new ArrayList<Country>();
public CountryArrayAdapter(Context context, int textViewResourceId,
List<Country> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.countries = objects;
}
public int getCount() {
return this.countries.size();
}
public Country getItem(int index) {
return this.countries.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
// ROW INFLATION
Log.d(tag, "Starting XML Row Inflation ... ");
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.country_listitem, parent, false);
Log.d(tag, "Successfully completed XML Row Inflation!");
}
// Get item
Country country = getItem(position);
countryIcon = (ImageView) row.findViewById(R.id.country_icon);
countryName = (TextView) row.findViewById(R.id.country_name);
countryAbbrev = (TextView) row.findViewById(R.id.country_abbrev);
countryName.setText(country.name);
String imgFilePath = ASSETS_DIR + country.resourceId;
try {
Bitmap bitmap = BitmapFactory.decodeStream(this.context.getResources().getAssets()
.open(imgFilePath));
countryIcon.setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countryAbbrev.setText(country.abbreviation);
return row;
}
private ArrayList<CountryAdapterListener> mListListener = new ArrayList<CountryAdapterListener>();
public void addListener(CountryAdapterListener aListener) {
mListListener.add(aListener);
}
private void sendListener(Country item, int position) {
for(int i = mListListener.size()-1; i >= 0; i--) {
mListListener.get(i).onClickNom(item, position);
}
}
/**
* Interface pour écouter les évènements sur le nom d'une personne
*/
public interface CountryAdapterListener {
public void onClickNom(Country item, int position);
}
} |
Partager