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