2016-07-29 88 views
0

我看了,並嘗試不同的方法,所以我絕對做錯了什麼。從另一個活動的sharedpreferences中檢索radiogroup選擇?

我在「磅」或「千克」的主要活動中有一個radiogroup選擇。無論他們選擇哪個收音機,都應在下一個活動中更改爲該文本。

Lbs是當前的默認無線電,並被選中爲「true」。

我已經在第二個活動中放置了一個按鈕返回到收音機選擇屏幕。但是,選擇「kgs」後,它會繼續顯示「lbs」。

主要活動

@Override 
public void onResume() { 
    super.onResume(); 

    SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); 
    String enteredWeightState = sharedPreferences.getString("entered_weight", ""); 
    int lbsState = sharedPreferences.getInt("UOM_checkId", UOM_RadioGroup.getCheckedRadioButtonId()); 

    UOM_RadioGroup.check(lbsState); 
    enter_weight_editText.setText(enteredWeightState); 

} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    UOM_RadioGroup = (RadioGroup) findViewById(R.id.UOM_RadioGroup); 
    enter_weight_editText = (EditText)findViewById(R.id.enter_weight_editText); 

    UOM_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

     public void onCheckedChanged(RadioGroup UOM_RadioGroup, int checkedId) { 

      if (checkedId == R.id.UOM_pounds) { 

       SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putInt("UOM_checkId", checkedId); 
       editor.apply(); 

      } else { 

       SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putInt("UOM_checkId", checkedId); 
       editor.apply(); 
      } 

     } 

    }); 
} 


    public void beginRoutineButton(View view) { 

     SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString("entered_weight", enter_weight_editText.getText().toString()); 
     editor.apply(); 

     Intent intent = new Intent(this, Activity2.class); 
     startActivity(intent); 
    } 

} 



Activity2 

    public TextView unit_of_measure; 
    public View fragment1; 
    public Button backButton; 
    public TextView weight; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_2); 

     weight = (TextView) findViewById(R.id.weight); 
     backButton = (Button) findViewById(R.id.backButton); 
     unit_of_measure = (TextView) findViewById(R.id.unit_of_measure); 

     SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); 

     int lbsState = sharedPreferences.getInt("UOM_checkId", -1); 
     String enteredWeightState = sharedPreferences.getString("entered_weight", ""); 


     weight.setText(enteredWeightState); 


     if (lbsState < 0) 
      unit_of_measure.setText("kgs"); 
     else 
      unit_of_measure.setText("lbs"); 


    } 


    public void backButton(View view) { 

     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 
    } 
} 

回答

0

做一個命名爲SharedPreferences,並在該類做出方法init和初始化的偏好有見下文: -

public class SharedPreferences{ 
    private static Context mContext; 
    private static SharedPreferences preferences = null; 
    private static SharedPreferences.Editor editor; 
    public static void init(Context mContext) { 
     SharedPreferences.mContext = Env.appContext; 
     SharedPreferences.preferences = mContext.getSharedPreferences("user_info", Context.MODE_PRIVATE); 
     SharedPreferences.editor = preferences.edit(); 
    } 
} 

,並調用SharedPreferences的init()方法中的發射活動作爲SharedPreferences.init(),並在如下的喜好中製造吸氣設置器: -

public static void setWeight(String weight){ 
editor.putString("entered_weight", weight).commit(); 
} 

public static String getWeight(){ 
return preferences.getString("entered_weight", null); 
} 

以及何時要設置權重呼叫SharedPreferences.setWeight(ed.getText.toString());並使用SharedPreferences.getWeight();獲得價值。您可以從應用程序內部的任何位置獲取價值,唯一需要做的就是調用啓動器活動的init()方法,並使用保存值並在sharedPreferences中獲取值。

像上面這樣會幫助你。

相關問題