android 屏幕旋转和实时更新gridview

发布于 2024-12-11 11:12:55 字数 2564 浏览 0 评论 0原文

我对内存泄漏和屏幕旋转有很大疑问。我知道当这种情况发生并开始新的活动时,活动就会被破坏。使用我的代码,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));
}

提前谢谢!

i have a big doubt about memory leak and screen rotation. I know that activity is destroy when that happens and new one is started. With my code, with is the best way to show the gridview? (this activity is part of a tab activity so in the main one i get the pictures from web services and put them on the object, using AsyncTask)

i would like to show the pictures at the same time they are saved on the object. Like simulating ajax...is that possible? at the moment, they are all show on the start or resume.

Also, im facing memory leak here?

last question, how can i show photos in the grid

my code:

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 in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-12-18 11:12:55

我认为你应该将其添加到你的活动中

android:configChanges="orientation" 

以避免旋转屏幕时的破坏问题...有了这个你不需要保存状态来恢复,没有必要,因为活动没有被破坏并且每次屏幕旋转发生变化时都会重新创建

I think you should add this to your activity

android:configChanges="orientation" 

to avoid the destroying problem when rotating the screen... with this you dont need to save state to restore, there is no need because the activity is not destroyed and recreated everytime the screen rotation changes

无尽的现实 2024-12-18 11:12:55

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

android:configChanges="orientation|screenSize"

For API 13+ the screenSize must also be handled:

android:configChanges="orientation|screenSize"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文