2015-02-23 81 views
0

我是新來的android編程,我只是想使用共享首選項將一些數據從一個活動發送到另一個活動,但我的代碼不起作用。Android - 使用共享首選項不工作發送數據

這是我在輸入數據的活動。

public class MainActivity extends ActionBarActivity { 

    private Button BTN; 
    private EditText EDTXT,EDTXT2,EDTXT3; 
    private CheckBox CHKBX; 
    private final String mName = "Middle Name"; 
    private final String fName = "First Name"; 
    private final String lName = "Last Name"; 
    private final String isReg = "Is Registered"; 

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

    BTN=(Button)findViewById(R.id.button); 
    CHKBX=(CheckBox)findViewById(R.id.checkBox); 
    EDTXT=(EditText)findViewById(R.id.editText); 
    EDTXT2=(EditText)findViewById(R.id.editText2); 
    EDTXT3=(EditText)findViewById(R.id.editText3); 

    BTN.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if(CHKBX.isChecked()){ 

       setPreferenceData(); 

       Intent INT = new Intent(MainActivity.this,SharedPreferenceData.class); 
       startActivity(INT); 

      } 

     } 
    }); 
} 

public void setPreferenceData(){ 

    SharedPreferences sp = this.getSharedPreferences("MyPref",MODE_WORLD_READABLE); 
    SharedPreferences.Editor spEditor = sp.edit(); 

    spEditor.putBoolean(isReg, CHKBX.isChecked()); 
    spEditor.putString(fName, EDTXT.getText().toString()); 
    spEditor.putString(mName, EDTXT2.getText().toString()); 
    spEditor.putString(lName, EDTXT3.getText().toString()); 
    spEditor.commit(); 


} 

這是我想要的數據傳遞活動。

public class SharedPreferenceData extends ActionBarActivity { 

private TextView TXTVW,TXTVW1,TXTVW2,TXTVW3; 
private final String fName = "First Name"; 
private final String mName = "Middle Name"; 
private final String lName = "Last Name"; 
private final String isReg = "Is Registered"; 

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

    TXTVW = (TextView) findViewById(R.id.textView); 
    TXTVW1 = (TextView) findViewById(R.id.textView2); 
    TXTVW2 = (TextView) findViewById(R.id.textView3); 
    TXTVW3 = (TextView) findViewById(R.id.textView4); 

    getSharedPreferencesData(); 

} 

public void getSharedPreferencesData(){ 

    SharedPreferences spf = this.getSharedPreferences("My Pref",MODE_WORLD_READABLE); 

    TXTVW.setText("First Name:" + spf.getString(fName,"")); 
    TXTVW1.setText("Middle Name:" + spf.getString(mName,"")); 
    TXTVW2.setText("Last Name:" + spf.getString(lName,"")); 
    TXTVW3.setText("Is Registered:" + spf.getBoolean(isReg,true)); 

} 
+2

爲什麼您的偏好名稱不同(「MyPref」與「My Pref」)?你爲什麼使用'MODE_WORLD_READABLE'? – tachyonflux 2015-02-23 18:42:33

回答

1

您可以將意向

Intent intent = new Intent(MainActivity.this, SharedPreferenceData.class); 
intent.putExtra(isReg, CHKBX.isChecked()); 
intent.putExtra(fName, EDTXT.getText().toString()); 
intent.putExtra(mName, EDTXT2.getText().toString()); 
intent.putExtra(lName, EDTXT3.getText().toString()); 
startActivity(intent); 
內傳遞數據

然後在onCreate(SharedPreferenceData中)內讀取它:

Intent intent = getIntent(); 
TXTVW.setText("First Name:" + intent.getStringExtra(fName,"")); 
TXTVW1.setText("Middle Name:" + intent.getStringExtra(mName,"")); 
TXTVW2.setText("Last Name:" + intent.getStringExtra(lName,"")); 
TXTVW3.setText("Is Registered:" + intent.getBooleanExtra(isReg,true)); 
0

您是否嘗試過使用默認的SharedPreferences?

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this); 

順便說一句,它會更好,如果你有你的地方中的常量來確定要發送的數據常量類:

public final class Constants { 

    // SHARED PREFERENCES 
    public static final String SHARED_PREF_F_NAME = "First Name"; 
    public static final String SHARED_PREF_M_NAME = "Middle Name"; 
    public static final String SHARED_PREF_L_NAME = "Last Name"; 
    public static final String SHARED_PREF_IS_REG = "Is Registered"; 
} 

然後你得到這些常量,如:

public void getSharedPreferencesData(){ 

    SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this); 

    TXTVW.setText("First Name:" + spf.getString(Constants.SHARED_PREF_F_NAME,"")); 
    TXTVW1.setText("Middle Name:" + spf.getString(Constants.SHARED_PREF_M_NAME,"")); 
    TXTVW2.setText("Last Name:" + spf.getString(Constants.SHARED_PREF_L_NAME,"")); 
    TXTVW3.setText("Is Registered:" + spf.getBoolean(Constants.SHARED_PREF_IS_REG,true)); 

}