我正在尝试学习android中的sharedPreferences并为此编写了一小段代码。但是一旦我执行它,应用程序就不会显示任何输出。
这是我的偏好类:
<Text>
<Text>We</Text><Text>Are</Text><Text>Inline</Text>
</Text>
这是我使用这个课程的主要活动:
public class SharedPreferncesExecutor {
public static final String PREFS_NAME = "AOP_PREFS";
public static final String PREFS_KEY = "AOP_PREFS_String";
public SharedPreferncesExecutor() {
super();
}
public void save(Context context, String Key, String Value) {
SharedPreferences settings;
SharedPreferences.Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
editor = settings.edit(); //2
editor.putString(Key, Value); //3
editor.commit(); //4
}
public String getValue(Context context,String key) {
SharedPreferences settings;
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(key, Context.MODE_PRIVATE);
text = settings.getString(key, null);
return text;
}
public void clearSharedPreference(Context context) {
SharedPreferences settings;
SharedPreferences.Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.clear();
editor.commit();
}
public void removeValue(Context context) {
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.remove(PREFS_KEY);
editor.commit();
}
}
这是我的activityMain.xml:
public class MainActivity extends Activity implements View.OnClickListener{
ImageButton bLogOut,bHowItWorks;
TextView SchoolDetail,AllClassDetail,TotalStudents,OneStudentDetail;
//this is for having access to userlocalstore to save/remove local data to user phone during login or Log out. So that if logout is pressed
//the data is wiped off the local storage on user mobile
UserLocalStore userLocalStore;
//Added to get the data from sharedPreferencesExecutor
private SharedPreferncesExecutor sharedPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//etEmail= (EditText)findViewById(R.id.etEmail);
//etPhoneNumber =(EditText)findViewById(R.id.etPhoneNumber);
bLogOut = (ImageButton) findViewById(R.id.bLogOut);
bLogOut.setOnClickListener(this);
userLocalStore = new UserLocalStore(this);
//added for QR codes scanner
bHowItWorks = (ImageButton) findViewById(R.id.bHowItWorks);
bHowItWorks.setOnClickListener(this);
SchoolDetail = (TextView)findViewById(R.id.SchoolDetail);
//Added to get the data from sharedPreferencesExecutor
sharedPreference = new SharedPreferncesExecutor();
Activity context = this;
sharedPreference.save(context,"SchoolData","KV Station;;;class1-secA;;;class1-secB;;;class1-secC;;;class4;;;class2-secA;;;class2-secB;;;class2-secC;;;class3-secA;;;class3-secB;;;class3-secC;;;class4-secA;;;class4-secB;;;class4-secC;;;class5-secA;;;class5-secB;;;class5-secC");
String SchoolData = sharedPreference.getValue(context,"SchoolData");
SchoolDetail.setText(SchoolData);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bLogOut:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
startActivity(new Intent(this,Login.class));
break;
case R.id.bHowItWorks:
startActivity(new Intent(this,howItWorks.class));
break;
}
}
@Override
protected void onStart() {
super.onStart();
if (authenticate() == true) {
displayUserDetails();
}
else{
Intent intentLogin = new Intent(MainActivity.this,Login.class);
intentLogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentLogin);
}
}
public boolean authenticate(){
return userLocalStore.getUserLoggedIn();
}
public void displayUserDetails(){
//User user = userLocalStore.getLoggedInUser();
//etEmail.setText(user.Email);
//etPhoneNumber.setText(user.PhoneNumber + "");
}
}
执行后,我没有收到任何数据。如果我遗失了某些内容,请告诉我
答案 0 :(得分:1)
检查您的SharedPreferencesExecutor
public String getValue(Context context,String key) {
SharedPreferences settings;
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
// here you are passing 'key' by mistake change it to 'PREFS_NAME'
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
text = settings.getString(key, null);
return text;
}
肯定它会工作..