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 |
public StationListAdapter(Context context) {
|
24 |
super();
|
25 |
this.context = context;
|
26 |
|
27 |
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
28 |
number.setMaximumFractionDigits(1);
|
29 |
number.setMinimumFractionDigits(1);
|
30 |
}
|
31 |
|
32 |
@Override
|
33 |
public int getCount() {
|
34 |
if (stations != null)
|
35 |
return stations.entries.size();
|
36 |
else
|
37 |
return 0;
|
38 |
}
|
39 |
|
40 |
@Override
|
41 |
public Object getItem(int position) {
|
42 |
return null;
|
43 |
}
|
44 |
|
45 |
public StationEntry getStation(int position) {
|
46 |
return stations.entries.get(position);
|
47 |
}
|
48 |
|
49 |
@Override
|
50 |
public long getItemId(int position) {
|
51 |
return position;
|
52 |
}
|
53 |
|
54 |
public void setStations(StationBean stations) {
|
55 |
this.stations = stations;
|
56 |
notifyDataSetChanged();
|
57 |
}
|
58 |
|
59 |
@Override
|
60 |
public View getView(int position, View convertView, ViewGroup parent) {
|
61 |
StationEntry station = stations.entries.get(position);
|
62 |
|
63 |
View root = inflater.inflate(R.layout.stationrow , parent, false);
|
64 |
TextView tview = (TextView) root.findViewById(R.id.stationName);
|
65 |
tview.setText(station.getName());
|
66 |
|
67 |
tview = (TextView) root.findViewById(R.id.stationDistance);
|
68 |
if (station.getCalcdist() != 0) {
|
69 |
tview.setText( context.getString(stationlistadapter_distance) + " " + number.format( (double)station.getCalcdist()/1000.0) + " km.");
|
70 |
} else {
|
71 |
tview.setVisibility(View.GONE);
|
72 |
}
|
73 |
|
74 |
root.findViewById(R.id.isregional).setVisibility( station.isRegional() ? View.VISIBLE : View.INVISIBLE );
|
75 |
root.findViewById(R.id.isstrain).setVisibility( station.isStrain() ? View.VISIBLE : View.INVISIBLE );
|
76 |
root.findViewById(R.id.ismetro).setVisibility( station.isMetro() ? View.VISIBLE : View.INVISIBLE );
|
77 |
|
78 |
return root;
|
79 |
}
|
80 |
}
|