2014-02-27 49 views
0

介紹保存對象的最後一個值狀態

我有一個繪畫活動,其中選擇從調色板我打開一個DialogFragment的顏色。

enter image description here

英鎊

我第一次打開DialogFragment,我instatiate的bnColor對象,我給它的第一個按鈕的價值,所以,在調色板,第一個按鈕會出現作爲選擇,這是我會畫的顏色。

但是,如果我然後在調色板中選擇另一種顏色,下次再次進入調色板時,我希望這種新顏色顯示爲選定的顏色。取而代之的是,總是選擇第一種顏色。

我知道這是因爲每次輸入DialogFragment時,bnColor對象都爲null,所以它總是獲得第一個顏色的值。爲了解決這個問題,我需要做一些事情,比如保存bnColor對象的最後一個狀態,所以當我輸入dialogFragment的時候,它會檢查是否是第一次,它是空的,或者我之前輸入並且有一個前一個值保存。

但我從來沒有做過這樣的事情,我不知道該怎麼做。

這是dialogFragment相關代碼:

private ImageButton bnColor; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    LayoutInflater inflater = LayoutInflater.from(getActivity()); 
    View view = inflater.inflate(R.layout.palette, null, false); 

    if (bnColor == null) { 
     LinearLayout drawLayout = (LinearLayout) view.findViewById(R.id.paint_colors); 
     bnColor = (ImageButton) drawLayout.getChildAt(0); 
     bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed)); 
    } 

回答

-1

你爲什麼不序列化bnColor object本身獲得其最後的狀態你可以做這樣的事情

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    LayoutInflater inflater = LayoutInflater.from(getActivity()); 
    View view = inflater.inflate(R.layout.palette, null, false); 

    ImageButton bnColor; 
    if(bnColor == null) 
    { 
     LinearLayout drawLayout = (LinearLayout) view.findViewById(R.id.paint_colors); 
     if(savedInstanceState!=null) 
      bnColor= (ImageButton) getResources().getLayout(savedInstanceState.getInt("color")); 
     else 
     bnColor = (ImageButton) drawLayout.getChildAt(0); 

     bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed)); 
    } 

    int value= bnColor.getId(); 

    savedInstanceState.putInt("color", value); 
    onSaveInstanceState(savedInstanceState); 
} 
+0

1)onSaveInstanceState什麼也不做。因此它無法工作。 2)在大多數情況下,savedInstanceState將爲空。只有當系統重新創建對話框時,它纔會爲空,而不是在您解散並顯示它時。 –

+0

@Doctoror Drive可以提供一個新答案的解決方案嗎? – masmic

+1

@ masmic_87在SharedPreferences中保存顏色並將其恢復到onCreateDialog。 –

0

轉到here,你會得到一個名爲CustomSharedPreference在源文件,只要下載並複製到你的身邊,並保存對象,就像您保存任何其他偏好對象

CustomSharedPreference mPrefs = new CustomSharedPreference(    getApplicationContext()); 
OR 
CustomSharedPreference mPrefs = new CustomSharedPreference("prefsName",Context.MODE_PRIVATE, getApplicationContext()); 

//Save your Object 
mPrefs.putObject("object", new Object()); 
//Retrieve it and store it 
Object o = (Object)mPrefs.getObject("object", null); 

注意:的觀點並沒有序列化,能夠使你將不得不創建一個自定義的ImageButton類像這樣的implements Serializable類,也MyImageButton內所有對象都應該已經實現了Serializable接口

public class MyImageButton extends ImageButton implements Serializable { 

    public MyImageButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 

    } 

} 

,並在你的XML

<com.example.MyImageButton 
    android:id="@+id/MyimageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/icon" /> 
0

使用同樣的我保存狀態這種方式。

public class MyDialogFragment extends DialogFragment { 

    //this is the default value, set when the dialog is created 
    private String myValue = "any initial String"; 
    private TextEdit myTextEdit; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     //construct the dialog 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     ViewGroup myView = (ViewGroup)inflater.inflate(R.layout.my_view, null); 
     builder.setView(myView); 

     //find the view and set the value 
     myTextEdit = (TextView)myView.findViewById(R.id.my_text_edit); 
     myTextEdit.setText(myValue); 

     return builder.create(); 
    } 

    @Override 
    public void onDismiss(DialogInterface dialog) { 
     super.onDismiss(dialog); 

     //when the dialog is dismissed, save the users input and overwrite the initial value 
     myValue = myTextEdit.getText(); 

    }