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