如何设置基础上,preferences值微调值 [英] How do I set spinner value based on the value in preferences

查看:166
本文介绍了如何设置基础上,preferences值微调值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充从jsonarray一个微调,我需要微调设置为存储在preferences值。我想设置微调字​​符串ID必须与idlocatie平等存储在preferences。

 共享preferences myShared preferences = preferenceManager.getDefaultShared preferences(本);
    最后弦乐地区= myShared preferences.getString(idlocatie,);
 JSONObject的jsonLocatii = JSONfunctions.getJSONfromURL(HTTP://www.mySite);
      尝试{
    JSONArray地震= jsonLocatii.getJSONArray(地震);        的for(int i = 0; I< earthquakes.length();我++){
            JSONObject的E = earthquakes.getJSONObject(I)
            字符串ID = e.getString(TAG_IDLOCATIE);
            字符串名称= e.getString(TAG_LOCATIE);
            locatiiList.add(新Locatii(ID,name.toUpperCase()));
            locatii =(微调)findViewById(R.id.spinLocatie);
            LocatiiAdapter cAdapter =新LocatiiAdapter(这一点,android.R.layout.simple_spinner_item,locatiiList);
            locatii.setAdapter(cAdapter);
        } }赶上(JSONException E){
     Log.e(log_tag,错误分析数据+ e.toString());
 }


解决方案

如果我的理解对不对,你要设置选定的价值?
尝试 locatii.setSelection(cAdapter.getPosition(locatie));

或者,你试图改变与选择相关的实际值?

修改:在这里,试试这个。你需要一些健全检查,但希望在正确的方向。
另一种选择是覆盖为getPosition(),和另一种是找到从locatiiList的索引,并使用该

 共享preferences myShared preferences = preferenceManager.getDefaultShared preferences(本);
最后弦乐地区= myShared preferences.getString(idlocatie,);
JSONObject的jsonLocatii = JSONfunctions.getJSONfromURL(HTTP://www.mySite);
尝试{
    JSONArray地震= jsonLocatii.getJSONArray(地震);    的for(int i = 0; I< earthquakes.length();我++){
        JSONObject的E = earthquakes.getJSONObject(I)
        字符串ID = e.getString(TAG_IDLOCATIE);
        字符串名称= e.getString(TAG_LOCATIE);
        locatiiList.add(新Locatii(ID,name.toUpperCase()));
    }
//将这些出的for循环。
    locatii =(微调)findViewById(R.id.spinLocatie);
    LocatiiAdapter cAdapter =新LocatiiAdapter(这一点,android.R.layout.simple_spinner_item,locatiiList);
    locatii.setAdapter(cAdapter); }赶上(JSONException E){
     Log.e(log_tag,错误分析数据+ e.toString());
 }locatii.setSelection(getIndex(locatii,locatie));私人诠释getIndex(微调微调​​,弦乐的myString){
    的for(int i = 0; I< spinner.getCount();我++){
        如果(spinner.getItemAtPosition(ⅰ)的ToString()。equalsIgnoreCase(的myString)){
            返回我;
        }
    }
    //检查这个,当你设置的位置。
    返回-1;
}

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

解决方案

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

这篇关于如何设置基础上,preferences值微调值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆