2016-04-21 62 views
1

我是Android中的法國noob,我有一個問題,我找不到解決方案。 實際上,我的平板電腦在旋轉期間無法保存我的LinearLayouts狀態。我已經嘗試了很多的東西,而且我做了這個代碼:當我打開我的平板電腦時,如何保存我的ImageView

public class Repas extends Activity { 

    private LinearLayout area1, area2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.repas); 
     area1 = (LinearLayout) findViewById(R.id.area1); 
     area2 = (LinearLayout) findViewById(R.id.area2); 

     DisplayMetrics dm = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 
     final int width_screen=dm.widthPixels; 
     area2.setMinimumWidth(width_screen); 

     if(savedInstanceState != null) { 
      int count_item = savedInstanceState.getInt("number_of_item"); 
      String image = savedInstanceState.getString("Sauvegarde_repas0"); 
      //area2.addView(image); 

     } 
     else { 
      TypedArray arrayResources = getResources().obtainTypedArray(
        R.array.resicon); 

      for (int i = 0; i < arrayResources.length(); i++) { 
       ImageView imageView = new ImageView(this); 
       imageView.setImageDrawable(arrayResources.getDrawable(i)); 
       imageView.setOnTouchListener(myOnTouchListener); 
       area2.addView(imageView); 
      } 

      arrayResources.recycle(); 
     } 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) 
{ 
    int count = area1.getChildCount(); 
    View v = null; 
    outState.putInt("number_of_item", count); 

for(int i=0; i<count; i++) { 
    v = area1.getChildAt(i); 
    outState.putString("Sauvegarde_repas"+i, v.toString()); 
} 
super.onSaveInstanceState(outState); 
} 

我現在的問題,假設我的邏輯是正確的,是我無法將字符串轉換成的ImageView我的平板電腦的旋轉過程中。你可以幫我嗎 ?

我也想知道我做的是對的:我的意思是讓我的佈局的元素一個接一個(我沒有找到如何保存整個佈局)?

我希望我很清楚:如果您需要更多信息,請不要猶豫!

非常感謝您的幫助!

+0

http://developer.android.com/guide/topics/resources/runtime-changes.htmlhttp://stackoverflow.com/questions/3915952/how-to-save-state-during-orientation-change-in -android-if-the-state-is-made-of-m http://stackoverflow.com/questions/12214600/save-data-and-change-orientation –

+0

謝謝你的回答!正如你所看到的,我已經使用onSavedInstanceState .... – ValentinLoricourt

回答

0

我做了一個新帖子,因爲我的代碼正在工作......但我有幾個問題,如果你能回答他們,我將非常感激。

這裏是我的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.repas); 
     area1 = (LinearLayout) findViewById(R.id.area1); 
     area2 = (LinearLayout) findViewById(R.id.area2); 


     TypedArray arrayResources = getResources().obtainTypedArray(
       R.array.resicon); 

     if(savedInstanceState != null) { 
      int count_item = savedInstanceState.getInt("number_of_item"); 
      int tab_item[] = new int[count_item]; 

      for (int i = 0; i < count_item; i++) { 
       tab_item[i]=savedInstanceState.getInt("Sauvegarde_repas" + i); 
      } 

      for (int i = 0; i < arrayResources.length(); i++) { 
       ImageView imageView = new ImageView(this); 
       imageView.setImageDrawable(arrayResources.getDrawable(i)); 
       imageView.setOnTouchListener(myOnTouchListener); 
       imageView.setId(i); 


       if (check(tab_item,i)){ 
        area1.addView(imageView); 
       } 
       else{ 
        area2.addView(imageView); 
       } 
      } 
     } 
     else { 

      for (int i = 0; i < arrayResources.length(); i++) { 
       ImageView imageView = new ImageView(this); 
       imageView.setImageDrawable(arrayResources.getDrawable(i)); 
       imageView.setOnTouchListener(myOnTouchListener); 
       imageView.setId(i); 
       area2.addView(imageView); 
      } 

      arrayResources.recycle(); 
     } 

     area1.setOnDragListener(myOnDragListener); 
     area2.setOnDragListener(myOnDragListener); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) 
    { 
     int count = area1.getChildCount(); 
     View v = null; 
     outState.putInt("number_of_item", count); 
     for(int i=0; i<count; i++) { 
      v = area1.getChildAt(i); 
      outState.putInt("Sauvegarde_repas" + i, v.getId()); 
     } 
     super.onSaveInstanceState(outState); 
    } 

這裏是我的盤問:

  • 當我設置一個ID,它只是 「我」,一個非常簡單的數字。它看起來不像字符串「R.id.String_here」那麼獨特(即使我知道它是以Integer轉換的),那麼我怎樣才能確保做一個唯一的ID?

  • 我必須一個一個保存我的佈局的孩子嗎?無法保存整個佈局?

    • 你有什麼建議來改進我的代碼嗎? (因爲它看起來髒髒的,是不是?)

非常感謝您的幫助!

相關問題