Ответ 1
Если я правильно понимаю, вы пытаетесь установить выбранное значение?
попробуйте locatii.setSelection(cAdapter.getPosition(locatie));
Или вы пытаетесь изменить фактическое значение, связанное с выбором?
EDIT. Здесь попробуйте это. Вам понадобится проверка здравомыслия, но, надеюсь, в правильном направлении. Другой вариант - переопределить getPosition(), а другой - найти индекс из locatiiList и использовать его.
SharedPreferences mySharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);
final String locatie= mySharedPreferences.getString("idlocatie", "");
JSONObject jsonLocatii = JSONfunctions.getJSONfromURL("http://www.mySite");
try {
JSONArray earthquakes = jsonLocatii.getJSONArray("earthquakes");
for(int i=0;i<earthquakes.length();i++){
JSONObject e = earthquakes.getJSONObject(i);
String id = e.getString(TAG_IDLOCATIE);
String name = e.getString(TAG_LOCATIE);
locatiiList.add(new Locatii(id, name.toUpperCase()));
}
// Move these out of the for loop.
locatii = (Spinner) findViewById(R.id.spinLocatie);
LocatiiAdapter cAdapter = new LocatiiAdapter(this, android.R.layout.simple_spinner_item, locatiiList);
locatii.setAdapter(cAdapter);
} catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
locatii.setSelection(getIndex(locatii, locatie));
private int getIndex(Spinner spinner, String myString) {
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(myString)){
return i;
}
}
// Check for this when you set the position.
return -1;
}