How do I set spinner value based on the value in preferences

空扰寡人 提交于 2019-12-11 13:58:06

问题


I populate a spinner from a jsonarray and i need to set the spinner to a value stored in preferences. I want to set the spinner String "id" to be equal with "idlocatie" stored in preferences.

  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()));        
            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());
 }

回答1:


if I understand it right, you're trying to set the selected value? try locatii.setSelection(cAdapter.getPosition(locatie));

Or are you trying to change an actual value associated with the selection?

EDIT: Here, try this. You'll need some sanity checking but hopefully on the right direction. Another option is to override getPosition(), and yet another is to find the index from locatiiList and use that.

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;
}


来源:https://stackoverflow.com/questions/25632549/how-do-i-set-spinner-value-based-on-the-value-in-preferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!