屏幕旋转和实时更新gridview android

时间:2011-10-21 13:05:01

标签: android gridview memory-leaks screen-orientation

我对内存泄漏和屏幕旋转存在很大疑问。我知道当发生这种情况并且新的活动开始时,活动就会被破坏。使用我的代码,with是显示gridview的最佳方式吗? (此活动是选项卡活动的一部分,因此在主要活动中,我从Web服务获取图片并使用AsyncTask将它们放在对象上)

我想在保存在对象上的同时显示图片。就像模拟ajax一样......可能吗?目前,他们都在开始或恢复时显示。

另外,我在这里面临内存泄漏?

最后一个问题,如何在网格中显示照片

我的代码:

public class LoteFotosActivity extends Activity {

Sistema sis=Sistema.getInstance();

Lote lote;
GridView gridview;
ImageAdapter iA;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lote_foto_view);
    Bundle extras = getIntent().getExtras();
    lote=sis.getLoteDetalle(extras.getInt("LoteID"));

    gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(iA=new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(LoteFotosActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });

}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return lote.FOTOS.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200,200));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(lote.FOTOS.get(position));
        return imageView;
    }

}
//test looking
public void onResume() { 
    super.onResume();
    Toast.makeText(this,"ON RESUME",
            Toast.LENGTH_SHORT).show(); 
//  gridview.setAdapter(new ImageAdapter(this));
//  foto.setImageDrawable(lote.FOTOS.get(0));
}
public void onPause() { 
    super.onPause();
    Toast.makeText(this,"onPause",
            Toast.LENGTH_SHORT).show(); 
    iA=null;
    gridview.setAdapter(iA=new ImageAdapter(this));
//  foto.setImageDrawable(lote.FOTOS.get(0));
}
提前thx!

2 个答案:

答案 0 :(得分:1)

我认为您应将此添加到您的活动中

android:configChanges="orientation" 

以避免在旋转屏幕时出现破坏问题...使用此功能,您无需保存要恢复的状态,因此无需因为每次屏幕旋转更改时都不会销毁和重新创建活动

答案 1 :(得分:1)

对于API 13+,还必须处理screenSize:

android:configChanges="orientation|screenSize"