如何在android中删除共享首选项值?

时间:2012-05-31 10:05:19

标签: android sharedpreferences

我想清除共享偏好设置中存储的值 我正在使用此代码。

/*  SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
            Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = myPrefs.edit();
            editor.clear();
            editor.commit();   */

但是得到了这个错误。

The method getSharedPreferences(String, int) is undefined for the type new View.OnClickListener(){}

private OnClickListener logoBarListener = new OnClickListener(){         / *          *(非Javadoc)          *          * @see android.view.View.OnClickListener#onClick(android.view.View)          * /         public void onClick(查看v){

        if (v.getId() == R.id.img_bottom_home) {

            showProgressBar(MainScreen.class);

        } else if (v.getId() == R.id.img_bottom_basket) {

            showProgressBar(ShopBasketGet.class);

        } else if (v.getId() == R.id.img_bottom_notification) {

            showProgressBar(Notification.class);

        } else if (v.getId() == R.id.img_bottom_login) {
            SharedPreferences myPrefs = getSharedPreferences("myPrefs",
                    MODE_WORLD_READABLE);
            SharedPreferences.Editor editor = myPrefs.edit();
            editor.clear();
            editor.commit();   

            showProgressBar();
        }
    }
};

8 个答案:

答案 0 :(得分:1)

删除this关键字(将this.getSharedPreferences()更改为getSharedPreferences()this指的是View.onClickListener()的内部类,而该方法实际上位于Activity类中。

答案 1 :(得分:0)

试试这种方式

 SharedPreferences pref = YourActivityName.this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
 pref.edit().clear().commit();

我认为你试图清除onclick事件而不是使用这种方式

SharedPreferences myPrefs = v.getContext().getSharedPreferences("myPrefs",Context.MODE_PRIVATE)
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();   

答案 2 :(得分:0)

使用以下代码段

    Context context=YourActivityName.this;
    SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",
    Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = myPrefs.edit();
    editor.clear();
    editor.commit();  

希望这有帮助,

VIPUL

答案 3 :(得分:0)

从单击按钮调用clearSharedPreferences()方法并将类变量上下文传递给此方法

public class MyLinearLayout extends LinearLayout {
    SharedPreferences preferences;
    SharedPreferences.Editor editor;
    Context context;
    public MyLinearLayout(Context context) {
        super(context);
            this.context = context;
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    private void clearSharedPreferences(Context context) {
        preferences = context.getSharedPreferences("myPrefs",
                context.MODE_PRIVATE);
        editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}

//您的代码将变为

if (v.getId() == R.id.img_bottom_home) {

            showProgressBar(MainScreen.class);

        } else if (v.getId() == R.id.img_bottom_basket) {

            showProgressBar(ShopBasketGet.class);

        } else if (v.getId() == R.id.img_bottom_notification) {

            showProgressBar(Notification.class);

        } else if (v.getId() == R.id.img_bottom_login) {
            clearSharedPreferences(context);
            showProgressBar();
        }
    }
};

答案 4 :(得分:0)

如果要在Activity类中创建LinearLayout类的对象,则应该传递 构造函数中的上下文。

以下是片段

活动类

public class SampleActivity extends Activity
{
    public void onCreate(Bundle bundle)
     {
         MyLinearLayout layout = new MyLinearLayout(this); 
         -----
         -----
     }  
}

MyLinearLayout类

public class MyLinearLayout extends LinearLayout {
    private Context context;
    public MyLinearLayout(Context context) {
        super(context);
        this.context=context; 
        SharedPreferences preferences=context.getSharedPreference("pref",
                context.MODE_PRIVATE);

    }

}

答案 5 :(得分:0)

Editor editor = getSharedPreferences("sharedPreferenceName", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();

答案 6 :(得分:0)

删除sharedPreference,一个var或所有sharedPreference ...

的示例
SharedPreferences sharedPreferences = getSharedPreferences("mysharedpreferences",MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
// a specific var of sharedPreference....
//Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called.
editor.remove("mySharedVar");

//if you want remove all sharedPreference...
//Mark in the editor to remove all values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor.
editor.clear();

editor.apply();

答案 7 :(得分:0)

您可以尝试以下代码。这些行对我有用。希望它也对您有用。 ^ _ ^

SharedPreferences preferences = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();

editor.clear();
editor.apply();