Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何基于首选项中的值设置微调器值_Android_Json_Spinner - Fatal编程技术网

Android 如何基于首选项中的值设置微调器值

Android 如何基于首选项中的值设置微调器值,android,json,spinner,Android,Json,Spinner,我从jsonarray填充微调器,需要将微调器设置为存储在首选项中的值。我想将微调器字符串“id”设置为与首选项中存储的“idlocatie”相等 SharedPreferences mySharedPreferences =PreferenceManager.getDefaultSharedPreferences(this); final String locatie= mySharedPreferences.getString("idlocatie", ""); JS

我从jsonarray填充微调器,需要将微调器设置为存储在首选项中的值。我想将微调器字符串“id”设置为与首选项中存储的“idlocatie”相等

  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());
 }
SharedReferences mySharedPreferences=PreferenceManager.GetDefaultSharedReferences(此选项);
final String locatie=mySharedPreferences.getString(“idlocatie”);
JSONObject jsonLocatii=JSONfunctions.getJSONfromURL(“http://www.mySite");
试试{
JSONArray地震=JSONLOCTAII.getJSONArray(“地震”);

对于(int i=0;i如果我理解正确,您是否尝试设置所选值? 尝试
locatii.setSelection(cAdapter.getPosition(locatie));

或者您正在尝试更改与选择关联的实际值

编辑:在这里,试试这个。你需要一些健全的检查,但希望方向正确。 另一个选项是重写getPosition(),另一个选项是从Locatilist查找索引并使用它

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;
}
SharedReferences mySharedPreferences=PreferenceManager.GetDefaultSharedReferences(此选项);
final String locatie=mySharedPreferences.getString(“idlocatie”);
JSONObject jsonLocatii=JSONfunctions.getJSONfromURL(“http://www.mySite");
试试{
JSONArray地震=JSONLOCTAII.getJSONArray(“地震”);

for(int i=0;我等待您的答案。我在首选项中存储了一个值,当我启动活动时,我希望使用mysql表中的值填充微调器,并将微调器选择的值设置为与我存储的值相等的值。我尝试了您的解决方案,但收到错误“方法getPosition(Locatii)”在类型中,ArrayAdapter不适用于参数(字符串)”。我的表有两列:idlocatie和locatie。谢谢,它正在工作,但我必须修改类Locatii,现在我的微调器显示id而不是名称。