如果我们更改方向模式,则重置GridVIew。

时间:2012-03-07 11:55:39

标签: android android-layout android-emulator

嗨,我正在为网格视图做简单的演示。我有关于改变布局的问题。

当我的演示在ladscape模式下工作时,它会存储复选框值。但是当我将方向更改为任何模式时。它再次调用onCreate()方法并重置所有值。任何人都可以建议我。如何在方向改变后保留值。

这是我的代码:

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

3 个答案:

答案 0 :(得分:0)

不是在oncreate中设置gridview的适配器,而是将所有代码放在onResume()中,它将正常工作..

答案 1 :(得分: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);
}

答案 2 :(得分:0)

您必须使用onSaveInstanceState(Bundle outState)方法将数据放入包中,然后使用onCreate方法恢复数据,如下所示:

if(savedInstance!=null){
    savedInstance.getInt(""); //or something you want
}