2011-06-17 88 views
0

我製作了一個Android應用程序,該應用程序可以獲取XML Feed(離開網站)並在其中顯示圖像。Android在應用程序狀態下將ImageView保存爲

我使用的是「畫廊」和「圖像適配器」顯示所有圖像 - 從網址加載它們(作爲原料提供了一個網址)

但是當過手機改變oriantation應用重新設置自己和狀態丟失 - 導致飼料再次消耗,圖像重新加載(這是非常大的帶寬)

在ImageAdapter我創建了一個ImageView對象的數組,允許我將從網站加載的圖像保留在本地對象上(以免再次加載它們)

但是sin每當手機改變方向時,應用程序就會重新設置,對象數組被清除,所有事情都會重新進行備份。

我想保存這個ImageView數組在應用程序狀態,但無法找到正確的功能?

下面是代碼:

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

請幫幫忙,我將如何去在App狀態保存這個???

回答

0

那是因爲你的ListView是內部的OnCreate()A.活動的方法定義 執行下列該活動,讓您在屏幕方向UIfreeze

添加以下活動申報清單:

android:configChanges="orientation" 

所以它看起來像

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

的問題是,該系統破壞活動時,發生配置更改。

感謝 迪帕克

+0

這工作 - 謝謝,雖然我是有點更關心的是試圖拯救的ImageView陣列進入App國... – Dagan 2011-06-17 12:22:10

相關問題