【问题标题】:How to Remove Shared Preference Values in android?如何在 android 中删除共享偏好值?
【发布时间】:2012-06-05 14:00:08
【问题描述】:

我想清除存储在共享首选项中的值 我正在使用此代码。

/*  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(){}

私有 OnClickListener logoBarListener = new OnClickListener() { /* *(非 Javadoc) * * @see android.view.View.OnClickListener#onClick(android.view.View) */ public void onClick(View 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();
        }
    }
};

【问题讨论】:

  • Shaiful 说的是对的。尝试使用 YourActivityName.this.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);

标签: android sharedpreferences


【解决方案1】:

去掉this关键字(将this.getSharedPreferences()改为getSharedPreferences()this指的是View.onClickListener()的内部类,而该方法实际上在Activity类中。

【讨论】:

  • 试试上面写的Shabbir Panjesha的方法。还有一个问题 - 你的课程扩展了 Activity,对吗?
  • 公共类 MyLinearLayout 扩展 LinearLayout { SharedPreferences 首选项; SharedPreferences.Editor 编辑器;公共MyLinearLayout(上下文上下文){超级(上下文); } public MyLinearLayout(Context context, AttributeSet attrs) { super(context, attrs);偏好 = context.getSharedPreferences("myPrefs", context.MODE_PRIVATE);编辑器 = 首选项.edit(); } }
【解决方案2】:

如果您在 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);

    }

}

【讨论】:

    【解决方案3】:

    使用下面的代码sn-p

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

    希望这会有所帮助,

    维普尔

    【讨论】:

    • 它也不起作用。因为我的班级没有扩展 Activity。它正在扩展线性布局。
    • 如果你从你的活动中调用你的类构造函数,那么你必须将上下文传递给这个构造函数。
    【解决方案4】:

    试试这个方法

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

    【讨论】:

    • 你是否尝试在活动或简单的java类或适配器类中使用sharedpreference@NiteshKabra
    【解决方案5】:

    通过单击按钮调用 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();
            }
        }
    };
    

    【讨论】:

      【解决方案6】:
      Editor editor = getSharedPreferences("sharedPreferenceName", Context.MODE_PRIVATE).edit();
      editor.clear();
      editor.commit();
      

      【讨论】:

      • 能否请您解释一下为什么这是解决问题的好方法。
      【解决方案7】:

      移除 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();
      

      【讨论】:

        【解决方案8】:

        你可以试试这堆代码。这些线条对我有用。希望它也对你有用。 ^_^

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-17
          • 2019-06-20
          • 2021-10-28
          • 2019-05-10
          • 2012-10-24
          • 2019-12-04
          相关资源
          最近更新 更多