2016-01-28 48 views
1

我想在我開發的viewpager中存儲按鈕最喜歡的狀態,以便用戶可以隨時回顧他們已經預訂了標記爲收藏的圖像。按鈕狀態被存儲,但是,一旦我重新打開應用程序,按鈕狀態沒有改變。是因爲活動破壞了嗎?如何在viewpager中存儲按鈕的狀態?Android按鈕狀態不存儲在viewpager中

@Override 
     public Object instantiateItem(final ViewGroup container, final int position) { 
      showProgress(); 

      imageView = (ImageView) findViewById(R.id.btn_favourite); 
      imageView.setColorFilter(Color.argb(255, 192, 192, 192)); 

      imageView.setOnClickListener(new View.OnClickListener() { 
       Boolean stateBtn= sharedPreference.getBtnState(context); 

       @Override 
       public void onClick(View v) { 
//     Boolean stateBtn= sharedPreference.getBtnState(context); 
        if(!stateBtn) { 
         sharedPreference.save(context, mUrl); 
         sharedPreference.saveBtnState(context, stateBtn); 
         Toast.makeText(context, 
           "Added to Favourite!", 
           Toast.LENGTH_SHORT).show(); 
         imageView.setColorFilter(Color.argb(255, 249, 0, 0)); 
        } 
        else 
        { 
         sharedPreference.saveBtnState(context, stateBtn); 
         imageView.setColorFilter(Color.argb(255, 192, 192, 192)); 
        } 
       } 
      }); 

      View photoRow = inflater.inflate(R.layout.item_image, container, 
        false); 

      ImageView image = (ImageView) photoRow.findViewById(R.id.img_flickr); 

      // added imageloader for better performance 
      StaggeredDemoApplication.getImageLoader().get(imageArrayList[position], 
        ImageLoader.getImageListener(image, R.drawable.bg_no_image, android.R.drawable.ic_dialog_alert), container.getWidth(), 0); 
      ((ViewPager) container).addView(photoRow); 
      stopProgress(); 

      return photoRow; 

     } 

這裏是共享偏好

public class SharedPreference { 

public static final String PREFS_NAME = "AOP_PREFS"; 
public static final String PREFS_STATE="AOP_BTN"; 
public static final String PREFS_KEY = "AOP_PREFS_String"; 
public static final String PREF_BTN_KEY = "AOP_PREF_BTN"; 

public SharedPreference() { 
    super(); 
} 

public void save(Context context, String text) { 
    SharedPreferences settings; 
    Editor editor; 

    //settings = PreferenceManager.getDefaultSharedPreferences(context); 
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1 
    editor = settings.edit(); //2 
    editor.putString(PREFS_KEY, text); //3 
    editor.commit(); //4 

} 

public String getValue(Context context) { 
    SharedPreferences settings; 
    String text; 

    //settings = PreferenceManager.getDefaultSharedPreferences(context); 
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
    text = settings.getString(PREFS_KEY, null); 

    return text; 
} 

public void clearSharedPreference(Context context) { 
    SharedPreferences settings; 
    Editor editor; 

    //settings = PreferenceManager.getDefaultSharedPreferences(context); 
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
    editor = settings.edit(); 

    editor.clear(); 
    editor.commit(); 
} 

public void removeValue(Context context) { 
    SharedPreferences settings; 
    Editor editor; 

    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
    editor = settings.edit(); 

    editor.remove(PREFS_KEY); 
    editor.commit(); 
} 

public void saveBtnState(Context context, Boolean stateBtn) { 

    SharedPreferences settings; 
    Editor editor; 

    //settings = PreferenceManager.getDefaultSharedPreferences(context); 
    settings = context.getSharedPreferences(PREFS_STATE, Context.MODE_PRIVATE); //1 
    editor = settings.edit(); //2 
    editor.putBoolean(PREF_BTN_KEY, stateBtn);//added state for button 
    editor.commit(); //4 

} 

public boolean getBtnState(Context context) 
{ 
    SharedPreferences prefs = context.getSharedPreferences(PREFS_STATE, Context.MODE_PRIVATE); 
    boolean switchState = prefs.getBoolean(PREF_BTN_KEY, false); 
    return switchState; 
} 
} 
+0

更改這個'布爾switchState = prefs.getBoolean(PREF_BTN_KEY,FALSE);' –

+0

@MadhukarHebbar,我已經改變,但仍然無法保存按鈕狀態 – michelletbs

+0

你需要寫你的'stateBtn'檢查的onClick方法中,使狀態將被保存onClick按鈕。 –

回答

1

變化的getBtnState方法

public boolean getBtnState(Context context) 
{ 
    SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
    boolean switchState = prefs.getBoolean(PREF_BTN_KEY, false); 
    return switchState; 
} 
+0

我已編輯,但仍然是相同的,可能把保存在活動中? – michelletbs

3

沒有,因爲你叫錯偏好的代碼。您必須使用PREFS_STATE而不是PREFS_NAME,並且還使用PREF_BTN_KEY而不是PREFS_NAME。這是因爲在保存按鈕狀態時,您正在使用首選項PREFS_STATE,並將布爾值與PREF_BTN_KEY一起使用。

public boolean getBtnState(Context context) 
{ 
    SharedPreferences prefs = context.getSharedPreferences(PREFS_STATE, Context.MODE_PRIVATE); 
    boolean switchState = prefs.getBoolean(PREF_BTN_KEY, false); 
    return switchState; 
} 
+0

@KDeongharkar,我已經改變了偏好,但仍然返回相同的...可能我需要把我的活動保存狀態? – michelletbs

+0

你有更改prefs.getBoolean(PREF_BTN_KEY,false);根據我的代碼。根據我的回答,您的getbtnstate中有兩處更改。請檢查@michelletbs – KDeogharkar

+0

@KDeongharkar,我已更改了代碼。我現在可以保存按鈕的狀態。一旦我向右或向左滑動圖像。按鈕狀態永遠不會變回默認狀態 – michelletbs