1 |
package dk.thoerup.traininfo;
|
2 |
|
3 |
import java.text.NumberFormat;
|
4 |
import java.util.List;
|
5 |
|
6 |
import android.content.Context;
|
7 |
import android.view.LayoutInflater;
|
8 |
import android.view.View;
|
9 |
import android.view.ViewGroup;
|
10 |
import android.widget.BaseAdapter;
|
11 |
import android.widget.TextView;
|
12 |
import static dk.thoerup.traininfo.R.string.*;
|
13 |
|
14 |
public class StationListAdapter extends BaseAdapter {
|
15 |
LayoutInflater inflater;
|
16 |
|
17 |
List<StationBean> stations;
|
18 |
Context context;
|
19 |
NumberFormat number = NumberFormat.getNumberInstance();
|
20 |
|
21 |
public StationListAdapter(Context context) {
|
22 |
super();
|
23 |
this.context = context;
|
24 |
|
25 |
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
26 |
number.setMaximumFractionDigits(1);
|
27 |
number.setMinimumFractionDigits(1);
|
28 |
}
|
29 |
|
30 |
@Override
|
31 |
public int getCount() {
|
32 |
if (stations != null)
|
33 |
return stations.size();
|
34 |
else
|
35 |
return 0;
|
36 |
}
|
37 |
|
38 |
@Override
|
39 |
public Object getItem(int position) {
|
40 |
return null;
|
41 |
}
|
42 |
|
43 |
public StationBean getStation(int position) {
|
44 |
return stations.get(position);
|
45 |
}
|
46 |
|
47 |
@Override
|
48 |
public long getItemId(int position) {
|
49 |
return position;
|
50 |
}
|
51 |
|
52 |
public void setStations(List<StationBean> stations) {
|
53 |
this.stations = stations;
|
54 |
notifyDataSetChanged();
|
55 |
}
|
56 |
|
57 |
@Override
|
58 |
public View getView(int position, View convertView, ViewGroup parent) {
|
59 |
StationBean station = stations.get(position);
|
60 |
|
61 |
View root = inflater.inflate(R.layout.stationrow , parent, false);
|
62 |
TextView tview = (TextView) root.findViewById(R.id.stationName);
|
63 |
tview.setText(station.getName());
|
64 |
|
65 |
tview = (TextView) root.findViewById(R.id.stationDistance);
|
66 |
if (station.getDistance() != 0) {
|
67 |
tview.setText( context.getString(stationlistadapter_distance) + " " + number.format( (double)station.getDistance()/1000.0) + " km.");
|
68 |
} else {
|
69 |
tview.setVisibility(View.INVISIBLE);
|
70 |
}
|
71 |
|
72 |
root.findViewById(R.id.isregional).setVisibility( station.isRegional() ? View.VISIBLE : View.INVISIBLE );
|
73 |
root.findViewById(R.id.isstrain).setVisibility( station.isSTrain() ? View.VISIBLE : View.INVISIBLE );
|
74 |
root.findViewById(R.id.ismetro).setVisibility( station.isMetro() ? View.VISIBLE : View.INVISIBLE );
|
75 |
|
76 |
return root;
|
77 |
}
|
78 |
}
|