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
| public class ListVille extends ArrayAdapter<String> {
private final Context context;
private final String[] ville;
TextView textView;
ImageView imageView;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
textView = (TextView) rowView.findViewById(R.id.label);
imageView = (ImageView) rowView.findViewById(R.id.logo);
//for(position=0; position<ville.length; position++) {
// JSONWeatherTask task = new JSONWeatherTask();
//task.execute(new String[]{ville[position]});}
textView.setText(ville[position]);
if(convertView == null ){
for(String str: ville ){
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String []{str});}}
else
rowView = (View)convertView;
return rowView;
}
public ListVille(Context context, String[] ville) {
super(context, R.layout.activity_main, ville);
this.context = context;
this.ville = ville;
}
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0]));
try {
weather = JSONWeatherParser.getWeather(data);
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
if ((weather.currentCondition.getIcon()).equals("03d")) {
imageView.setImageResource(R.drawable.image03d);
}
else if ((weather.currentCondition.getIcon()).equals("01d")) {
imageView.setImageResource(R.drawable.image01d);
}
else if ((weather.currentCondition.getIcon()).equals("02d")) {
imageView.setImageResource(R.drawable.image02d);
}
else if ((weather.currentCondition.getIcon()).equals("04d")) {
imageView.setImageResource(R.drawable.image04d);
}
else if ((weather.currentCondition.getIcon()).equals("50d")) {
imageView.setImageResource(R.drawable.image50d);
}
else if ((weather.currentCondition.getIcon()).equals("09d")) {
imageView.setImageResource(R.drawable.image09d);
}
else if ((weather.currentCondition.getIcon()).equals("10d")) {
imageView.setImageResource(R.drawable.image10d);
}
else if ((weather.currentCondition.getIcon()).equals("11d")) {
imageView.setImageResource(R.drawable.image11d);
}
else if ((weather.currentCondition.getIcon()).equals("13d")) {
imageView.setImageResource(R.drawable.image13d);}
else {
imageView.setImageResource(R.drawable.ic_launcher);
}
}
}
} |