由于某些原因,共享首选项不起作用

时间:2019-06-07 22:29:45

标签: java android sharedpreferences

我创建了一个活动,该活动将数据发送到mySQL数据库,并且一切正常运行,但是我有一个sharedpreference类,该类工作正常,应该可以将我从涉及该问题的类中发送到名为accueil的活动,但是由于某种原因,共享首选项为null,并且由于Accueil类条件,它会发送到登录活动,而不是

这是我的共享偏好设置类

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

1 个答案:

答案 0 :(得分:0)

您没有使用共享首选项编辑器正确保存数据。投入价值后,您应该进行一次commit

    editor.putBoolean(HISTO, b);
    editor.commit();
相关问题