105-自訂三欄式ListView(行動裝置進階應用)

1. GAD01.java

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;

public class GAD01 extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.main);

ArrayList<Weather> weatherList = new ArrayList<Weather>();

// Create Weather object add weather data to weatherList
Weather taipei = new Weather("台北", 23, Weather.OVERCAST);
Weather taoyuan = new Weather("桃園", 26, Weather.OVERCAST);
Weather hsinchu = new Weather("新竹", 29, Weather.SUNNY);
Weather taichung = new Weather("台中", 27, Weather.RAIN);
weatherList.add(taipei);
weatherList.add(taoyuan);
weatherList.add(hsinchu);
weatherList.add(taichung);

// assign adapter to listAdapter
WeatherAdapter weatherAdapter = new WeatherAdapter(this, weatherList);
this.setListAdapter(weatherAdapter);
}

}

2. Weather.java

public class Weather {
public static final int NA = -1;
public static final int SUNNY = 0;
public static final int OVERCAST = 1;
public static final int RAIN = 2;

public String city = null;
public int temperature = 0;
public int sky = NA;

public Weather(String city, int temperature, int sky) {
this.city = city;
this.temperature = temperature;
this.sky = sky;
}

public String getCity() {
return city;
}

public int getTemperature() {
return temperature;
}

public int getSkyResource() {
switch (sky) {
case SUNNY:
// return sunny image drawable
return R.drawable.sunny;

case OVERCAST:
// return overcast image drawable
return R.drawable.overcast;

case RAIN:
// return rain image drawable
return R.drawable.rain;
}
return 0;
}

}

3. WeatherAdapter.java

import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class WeatherAdapter extends BaseAdapter {

private Context context;
private List<Weather> weatherList;

// build the constructor

public WeatherAdapter(Context context, List<Weather> weatherList) {
this.context = context;
this.weatherList = weatherList;
}

public int getCount() {
return weatherList.size();
}

public Object getItem(int position) {
return weatherList.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
Weather weather = weatherList.get(position);
return new WeatherAdapterView(this.context, weather);
}
}

4. WeatherAdapterView.java

import android.content.Context;
import android.graphics.Color;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherAdapterView extends LinearLayout {

public WeatherAdapterView(Context context, Weather weather) {

super(context);
this.setOrientation(HORIZONTAL);

// city
// let city name on the left
LinearLayout.LayoutParams cityLayoutParams = new LinearLayout.LayoutParams(
100, LayoutParams.WRAP_CONTENT);
cityLayoutParams.setMargins(1, 10, 1, 10);
TextView citys = new TextView(context);
citys.setText(weather.getCity());
citys.setTextSize(14f);
citys.setTextColor(Color.WHITE);
addView(citys, cityLayoutParams);

// temperature
// let temperature in the middle
// setTextSize(14f)
LinearLayout.LayoutParams temperatureLayoutParams = new LinearLayout.LayoutParams(
40, LayoutParams.WRAP_CONTENT);
temperatureLayoutParams.setMargins(1, 10, 1, 10);
TextView temperature = new TextView(context);
temperature.setText(Integer.toString(weather.temperature));
temperature.setTextColor(Color.WHITE);
temperature.setTextSize(14);
this.addView(temperature, temperatureLayoutParams);

// weather
// let weather image place in right side
LinearLayout.LayoutParams weatherLayoutParams = new LinearLayout.LayoutParams(
40, LayoutParams.WRAP_CONTENT);
weatherLayoutParams.setMargins(140, 10, 10, 10);
ImageView imageView = new ImageView(context);
imageView.setImageResource(weather.getSkyResource());
this.addView(imageView, weatherLayoutParams);
}
}

沒有留言: