Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Folks;</p> <p>After lots of looking and trying and testing and doing hundread of other things I answered my own question.</p> <p>This is something that may be a Doh! answer for programmers, but I am not a programmer, I am learning and doing this as a school project, so my knowledge of Java is not even enought to be called "basic".</p> <p>This is the answer to my own question;</p> <pre><code>String CountryName = preferences.getString("country name", null); List&lt;String&gt; lCounties = Arrays.asList(Counties(CountryName).split(",")); CharSequence[] csCounties = lCounties.toArray(new CharSequence[lCounties.size()]); final Spinner sCounty = (Spinner) findViewById(R.id.county); ArrayAdapter&lt;CharSequence&gt; aCounty = new ArrayAdapter&lt;CharSequence&gt;( this, android.R.layout.simple_spinner_item,csCounties); aCounty.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sCounty.setAdapter(aCounty); sCounty.setSelection(preferences.getInt("county",0)); sCounty.setOnItemSelectedListener(new OnItemSelectedListener(){ public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3){ int iCounty = sCounty.getSelectedItemPosition(); String CountyName = sCountry.getItemAtPosition(arg2).toString(); SharedPreferences.Editor editor = preferences.edit(); editor.putString("county name", CountyName); editor.putInt("county", iCounty); editor.commit(); //Refresh Page is county has changed if (oCounty != iCounty){ refresh(); } } public void onNothingSelected(AdapterView&lt;?&gt; arg0) {} }); </code></pre> <p>Now this is the explanation (as far as I undertand...)</p> <p>First I read the country from another spinner (I have 'few' spinners joined together)</p> <pre><code>String CountryName = preferences.getString("country name", null); </code></pre> <p>Then I use this information to call a subclass (I can publish this too). This subclass contacts the server to create a MySQL query an retrieves the needed information; in this case the counties (or states) for a given country, the list is received as state1,state2,state3, [this was formated on the PHP on the server to help]</p> <pre><code>List&lt;String&gt; lCounties = Arrays.asList(Counties(CountryName).split(",")); CharSequence[] csCounties = lCounties.toArray(new CharSequence[lCounties.size()]); </code></pre> <p>The next lines of code are the actual Spinner, there is alot of information about spinner on the net, so I will not explain much of this.</p> <p>The spinner read the saved information using this line;</p> <pre><code>sCounty.setSelection(preferences.getInt("county",0)); </code></pre> <p>this allows the spinner to be pre-populated with the users choice.</p> <p>Then I store the selection, I store both the name and pointer. The name is saved to complete the URL when the page is loaded and the pointer is saved bacuase the Spinner seems to like pointers.</p> <pre><code> SharedPreferences.Editor editor = preferences.edit(); editor.putString("county name", CountyName); editor.putInt("county", iCounty); editor.commit(); </code></pre> <p>The last thing I do is a comparison, the varible <code>oCounty</code> is read when the page loads, it simply reads the <code>preference</code> file and stores the value on <code>oCounty</code> (Original County), if the county name changes, then it calls <code>refresh()</code>, this subclass refreshes the page. [This too was a pain to put together for me]</p> <pre><code> //Refresh Page is county has changed if (oCounty != iCounty){ refresh(); } </code></pre> <p>The way the whole thing works now is that once a country is selected from the list, the page goes to the server, loads the counties/states for the country and refreshed the page, thus loading the values on the spinner, if the country changes, the process will load the counties/states again.</p> <p>The same code is used with the counties/states to load the cities, I jsut changed the variable's names.</p> <p>I am sure programmers have a nicer, faster, quicker way to do this, but these are my 2 cents on how to populate spinners with iformation stored on a MySQL database on the server.</p> <p>Regards; Ramón</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload