2012-05-31 46 views
1

我想清除存储在共享首选项中的值 我正在使用此代码。如何删除android中的共享偏好值?

/* 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 =新OnClickListener(){ /* * (非的Javadoc) * * @see android.view.View.OnClickListener#的onClick(android.view.View) */ 公共无效的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(); 
     } 
    } 
}; 
+0

什么Shaiful说的是right.Try使用YourActivityName.this.getSharedPreferences( “myPrefs”,Context.MODE_PRIVATE); –

+0

它也不起作用。 –

回答

0

如果您要创建您的LinearLayout对象在Activity类中的类,那么你应该在构造器中传递 上下文。

下面是剪断

活动类

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

    } 

}

1

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

+0

问题没有解决.error仍然存在。 –

+1

发布您的完整代码。 – Shaiful

+0

尝试上面写的Shabbir Panjesha方法。还有一个问题 - 你的课程扩展了活动,对吧? – Shaiful

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

它不工作。在getSharedPreferences上显示错误。 –

+0

是你尝试使用sharedpreference中的活动或在简单的java类或适配器类@ NiteshKabra – Khan

0

使用下面的代码片段

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

希望这有助于

Vi PUL

+0

它也不能正常工作。因为我的课没有扩展Activity.it扩展了线性布局。 –

+0

如果您从您的活动中调用您的类构造函数,那么您必须将上下文传递给此构造函数。 –

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(); 
     } 
    } 
}; 
0
Editor editor = getSharedPreferences("sharedPreferenceName", Context.MODE_PRIVATE).edit(); 
editor.clear(); 
editor.commit(); 
+0

您能不能请解释为什么这是解决问题的好办法。 –

0

例子为删除sharedPreference,一个变种或全部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();