2012-03-07 61 views
1

嗨,我正在做網格視圖的簡單演示。我有關於改變佈局的問題。如果我們改變方向模式,它重置GridVIew。

當我的演示在ladscape模式下工作時,它存儲複選框的值。但是當我改變方向到任何模式。它再次調用onCreate()方法並重置所有值。可以anybdy建議我。在方向發生變化後如何保留值。

這裏是我的代碼:

btnSubmit = (Button) findViewById(R.id.editButton); 
     btnSubmit.setOnClickListener(this); 


     gridview.setAdapter(new ImageAdapter(this, status)); 
     gridview.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 

       ImageView imgview = (ImageView) v.findViewById(R.id.imgvew); 
       imgview.setImageResource(R.drawable.check_box_checked); 
      } 

     }); 



@Override 
    public void onConfigurationChanged(android.content.res.Configuration newConfig) { 

     if (newConfig.ORIENTATION_LANDSCAPE == Configuration.ORIENTATION_PORTRAIT){ 

     } 
    }; 


@Override 
    public void onClick(View v) { 
       status = true; 
     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this, status)); 
      } 

回答

0

而是在OnCreate中把所有的代碼中的onResume(),它會正常工作設置你的GridView的適配器..

+0

03-07 17:46:50.303:E/AndroidRuntime(1112):了java.lang.RuntimeException:無法恢復活動{com.thumbnail/com.thumbnail.ThumbnailProjectActivity}:android.app.SuperNotCalledException:活動{c om.thumbnail/com.thumbnail.ThumbnailProjectActivity}沒有調用super.onResume() 03-07 17:46:50.303:E/AndroidRuntime(1112):\t at android.app.ActivityThread.performResumeActivity(ActivityThread.java :2215) 03-07 17:46:50.303:E/AndroidRuntime(1112):\t at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2243) 03-07 17:46:50.303:E/AndroidRuntime 1112):\t at – 2012-03-07 12:20:05

+0

Put super.onResume();到您的overres的onResume方法結束 – 2012-03-07 17:23:17

0

,你需要重寫兩個方法:

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    // Read values from the "savedInstanceState"-object and put them in your textview 
    if (icicle != null) 
    { 
     value = icicle.getLong("param"); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    // Save the values you need from your textview into "outState"-object 
    outState.putLong("param", value); 
    super.onSaveInstanceState(outState); 
} 
相關問題