android - How to display the distance and time travelled between two points using spinners? -
i want create app selecting town-a 1 spinner , town-b other spinner. want display distance between towns time take there selected towns.
i have found this:
var dist = [ ['town b', [], []], ['town c', [67], ['1h00m']], ['town d', [282,251], ['11h10m','10h00m']], ['town e', [243,210,41], ['9h45m','8h25m','1h40m']],
now html dropdown boxes , calculates above file (dist.js)
now know how convert using 2 spinners. think have basic idea not sure how implement. thinking when spinner 1 selected , spinner 2 needs spinner 1 = spinner 2 , distance 67km , time 1h00m.
i don't have code cause haven't tried yet because not sure start. hoping me?
edit
this have sofar:
public class mainactivity extends activity implements onitemselectedlistener { private spinner startloc; private spinner enddes; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); startloc = (spinner) findviewbyid(r.id.spinner1); arrayadapter<charsequence> adapter_start = arrayadapter .createfromresource(this, r.array.start_location, android.r.layout.simple_spinner_item); adapter_start.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); startloc.setadapter(adapter_start); startloc.setonitemselectedlistener(this); enddes = (spinner) findviewbyid(r.id.spinner2); arrayadapter<charsequence> adapter_end = arrayadapter .createfromresource(this, r.array.end_location, android.r.layout.simple_spinner_item); adapter_end.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); enddes.setadapter(adapter_end); enddes.setonitemselectedlistener(this); } @override public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub }
and this:
<string-array name="start_location"> <item>town a</item> <item>town b</item> <item>town c</item> </string-array> <string-array name="end_location"> <item>town a</item> <item>town b</item> <item>town c</item> </string-array> <string-array name="distance"> <item>0</item> <item>67</item> <item>282</item> <item>251</item> </string-array> <string-array name="time"> <item>0m</item> <item>2h40m</item> <item>11h10m</item> <item>10h00m</item>
so means: town town 0 , time 0, town town b 67km , time 2h40m, town town c 282km 11h10m , town b town c 251km , time 10h00m. (and needs work in reverse of list order well)
now how do this?
screen shot:
i think may hung on how data populates spinner works in android.
a spinner in android uses adapter fill spinner backing data. data collection of custom objects, strings, integers, etc... spinner can register listener when item selected spinner callback made indicates item selected.
a simple implementation use arrays of strings populate spinners. when selection made spinner1, you'd store value somewhere. when selection made spinner2 you'd store value , check see selection had been made spinner1. if selection had been made each spinner, you'd able distance / time each selection. simple way use if-else statements:
if (spinnerone.equals("denver")) { if (spinnertwo.equals("new york") { //print out distance equals 1280 miles } else if (spinnertwo.equals("san fransisco") { //etc... } } else if (spinnerone.equals("new york") { //etc... }
the implementation describe above super simple way of doing , work illustration. there better ways of structuring data.
Comments
Post a Comment