Android在App状态下保存ImageView

时间:2011-06-17 11:02:18

标签: android imageview state

我制作了一个Android应用程序,它通过它循环访问XML Feed(并在网站上)并在Feed中显示图像。

我使用“图库”和“图片适配器”来显示所有图片 - 从网站加载图片(因为Feed提供了网址)

但是当电话改变时,应用程序会自行重置并且状态丢失 - 导致再次消耗馈送并重新加载图像(带宽非常大)

在ImageAdapter中,我创建了一个ImageView对象数组,允许我将从网站加载的图像保留在本地对象上(所以不要再加载它们)

但是,由于每次手机更改方向时都会重新设置应用程序,因此对象阵列会被删除,所有内容都会再次加载备份。

我正在尝试将此ImageView数组保存在App状态但无法找到正确的函数???

以下是代码:

package com.test;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;


public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    private String[] mImageIds = {};
    private String[] titles = {};
    public ImageView[] images = {};

    public ImageAdapter(Context c) {
        mContext = c;

        try{
            //open local file and parse to XML  
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc;

            doc = docBuilder.parse(mContext.openFileInput("xmlfeed.xml"));
            doc.getDocumentElement().normalize();

            NodeList itms = doc.getElementsByTagName("item");
            String temp = "";
            String tempTitles = "";

            //loop through xml items to retreive all nodes
            for (int i = 0; i < itms.getLength(); i++){
                Node itm = itms.item(i);
                if(temp==""){
                    temp = getTagValue(itm, "image");
                    tempTitles = getTagValue(itm, "title");
                }else{
                    temp = temp + "," + getTagValue(itm, "image");
                    tempTitles = tempTitles + "," + getTagValue(itm, "title");
                }
            }
            mImageIds = temp.split(",");
            titles = tempTitles.split(",");

            //create all images as empty vars
            if(images.length==0){
                images = new ImageView[itms.getLength()];
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getCount() {
        return mImageIds.length;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        //loads the image in case it does not already exists in the object
        if(images[position]==null){
            ImageView imgs = new ImageView(mContext);
            try{
                imgs.setImageDrawable(drawable_from_url(mImageIds[position], "src"));
            }catch(Exception e){
                e.printStackTrace();
            }
            imgs.setScaleType(ImageView.ScaleType.FIT_XY);
            images[position] = imgs;
        }
        return images[position];
    }

    Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException{
        Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();
        return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);
    }

    public String getTagValue(Node itm, String tagName){
        String title_value = "";
        if (itm.getNodeType() == Node.ELEMENT_NODE)
        {
            Element itmElm = (Element) itm;
            NodeList title = itmElm.getElementsByTagName(tagName);
            Element titleElm = (Element) title.item(0);
            NodeList title_nodes = titleElm.getChildNodes();
            title_value = ((Node) title_nodes.item(0)).getNodeValue().trim();
        }
        return title_value;
    }
}

请帮助,我将如何在应用程序状态中保存?

1 个答案:

答案 0 :(得分:0)

这是因为你的listview是在Activity A的oncreate()方法中定义的。 执行以下活动以使UIfreeze处于屏幕方向

将以下内容添加到清单中的活动声明:

android:configChanges="orientation"

所以它看起来像

<activity android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden" 
        android:name=".your.package">

问题是,当配置发生变化时,系统会破坏活动。

由于 迪帕克