gpt4 book ai didi

java - 由于某种原因,共享偏好不起作用

转载 作者:行者123 更新时间:2023-12-02 04:43:25 25 4
gpt4 key购买 nike

我创建了一个将数据发送到 mySQL 数据库的 Activity ,并且一切都在这一侧工作正常,但我有一个可以工作的共享首选项类,该类应该将我发送到相关类中名为 acueil 的 Activity ,该 Activity 称为 contact 但由于某种原因,共享首选项为 null,并且由于 Accueil 类条件 if

,它会发送到登录 Activity

这是我的共享偏好类

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

以及我正在研究的联系类(class)

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

最佳答案

您没有使用共享首选项编辑器正确保存数据。输入您的值后,您应该进行提交:

    editor.putBoolean(HISTO, b);
editor.commit();

关于java - 由于某种原因,共享偏好不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56501866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com