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

标签 java android sharedpreferences

我创建了一个将数据发送到 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/

相关文章:

java - Watson 视觉识别 - 未经授权 : Access is denied due to invalid credentials

java - 如何使用java将一个Excel单元格拆分为两个单元格

java - JSF:如何向支持 bean 中的组件添加 ajax 功能?

flutter - Flutter接收Future数据(SharedPreferences)的问题

java - 如何在 WSDL-first 方法中使用一些内容编写 DTO(例如 : validation) method? 只是不写 "anemic domain model"

Android,变音符号不敏感 SQLite 搜索

android - 非 Activity 时如何访问窗口?

java - Android Search ArrayList of objects for only a parameter,返回对象索引

android - 以数组列表或其他形式将待定 Intent 保存到共享首选项中?

android - 从对话框中保存首选项