[英]for some reason the shared preference is not working
我創建了一個將數據發送到mySQL數據庫的活動,並且everthing在這一方面工作但是我有一個共享偏好類,它可以將我發送到一個名為accueil的活動,這個活動叫做聯系但是由於某種原因共享首選項為null,它會發送到登錄活動,因為accueil類條件if
這是我的共享偏好類
public class SharedPrefManager {
private static final String SHARED_PREF_NAME = "CIN";
private static final String IDCONT = "con";
private static final String HISTO = "historique";
private static SharedPrefManager mInstance;
private static Context ctx;
private SharedPrefManager(Context context) {
ctx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
public void setIdCont(String id) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(IDCONT, id);
editor.apply();
}
//this method will give the logged in user
public String getIdCont() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(IDCONT, null);
}
//this method will give the logged in user
public void setHisto(boolean b) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(HISTO, b);
editor.apply();
}
//this method will give the logged in user
public Boolean getHisto() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(HISTO, false);
}
//this method will logout the user
public void logout() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}
和我正在研究的聯系班
public class SharedPrefManager {
private static final String SHARED_PREF_NAME = "CIN";
private static final String IDCONT = "con";
private static final String HISTO = "historique";
private static SharedPrefManager mInstance;
private static Context ctx;
private SharedPrefManager(Context context) {
ctx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
public void setIdCont(String id) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(IDCONT, id);
editor.apply();
}
//this method will give the logged in user
public String getIdCont() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(IDCONT, null);
}
//this method will give the logged in user
public void setHisto(boolean b) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(HISTO, b);
editor.apply();
}
//this method will give the logged in user
public Boolean getHisto() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(HISTO, false);
}
//this method will logout the user
public void logout() {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}
和accueil類,由於條件其他重定向我登錄
if(SharedPrefManager.getInstance(getApplicationContext()).getIdCont() != null) {
String[] listviewTitle = new String[]{
"Consulter historique eau",
"Consulter historique electicite",
"ajouter taux de consommation",
"se deconecter",
"contacter service clientele"
};
int[] listviewImage = new int[]{
R.drawable.ic_pipe,
R.drawable.ic_battery,
R.drawable.ic_ticket,
R.drawable.ic_exit,
R.drawable.ic_send_message,
};
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 5; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}
String[] from = {"listview_image", "listview_title"};
int[] to = {R.id.item_image, R.id.item_title};
SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(), aList, R.layout.acceuil_adapter, from, to);
ListView liste = (ListView) findViewById(R.id.list_view);
liste.setAdapter(simpleAdapter);
liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Intent intent;
intent = new Intent(acceuilAct.this, historique.class);
startActivity(intent);
}else if (position==1){
Intent intent;
intent = new Intent(acceuilAct.this, historiqueelec.class);
startActivity(intent);
}else if (position==2){
Intent intent;
intent = new Intent(acceuilAct.this, addInfo.class);
startActivity(intent);
}else if (position==3){
SharedPrefManager.getInstance(getApplicationContext()).logout();
Intent intent = new Intent(acceuilAct.this,LoginActivity.class);
startActivity(intent);
finish();
}
else if (position==4){
SharedPrefManager.getInstance(getApplicationContext()).logout();
Intent intent = new Intent(acceuilAct.this,contact.class);
startActivity(intent);
finish();
}
}
});
}
else{
Intent intent = new Intent(acceuilAct.this,LoginActivity.class);
startActivity(intent);
finish();
}
您沒有使用共享首選項編輯器正確保存數據。 在你提出你的價值后你應該做一個提交ex:
editor.putBoolean(HISTO, b);
editor.commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.