2012-07-28 80 views
0

我正在構建一個包含列表的應用程序,列表中的每個項目都有一些值(名稱,說明和日期)。我製作了一個XML文件,其中包含列表中任何項目的結構。
我也有另外一個XML文件,它包含在列表中的項目(每個<item>標籤都有<name><desc><date>兒童)
問題是我不知道如何把一切都在正確的地方。我」已經搜索到它的網站和我發現,這就是所謂的XML解析,但我發現的唯一教程this one,但它是寫不清楚的,所以我不明白它..
有人能解釋它還是給了我一個很好的教程?XML解析(將數據從XML移動到列表)

編輯:
This is結構的XML文件
This is內容的XML文件

回答

2

您設置爲string-array的數據未正確構建以顯示在ListView中。它應該是這樣的:

<string-array name="exams"> 
     <item>@array/exam1</item> 
     <item>@array/exam2</item> 
     <item>@array/exam3</item> 
     <item>@array/exam4</item> 
    </string-array> 
    <string-array name="exam1"> 
     <item>One</item> 
     <item>11111111One</item> 
     <item>25/7/12</item> 
    </string-array> 
    <string-array name="exam2"> 
     <item>Two</item> 
     <item>2222222222Two</item> 
     <item>28/7/12</item> 
    </string-array> 
    <string-array name="exam3"> 
     <item>Three</item> 
     <item>333333333333Three</item> 
     <item>29/1/10</item> 
    </string-array> 
    <string-array name="exam4"> 
     <item>Four</item> 
     <item>444444444Four</item> 
     <item>21/2/11</item> 
    </string-array> 

在數據結構很好解析此爲ListView你會寫(代碼的一部分來自這樣的回答:Android Resource - Array of Arrays):

Resources res = getResources(); 
     ArrayList<Exam> extractedData = new ArrayList<Exam>(); 
     TypedArray ta = res.obtainTypedArray(R.array.exams); 
     int n = ta.length(); 
     for (int i = 0; i < n; ++i) { 
      int id = ta.getResourceId(i, 0); 
      if (id > 0) { 
       extractedData.add(new Exam(res.getStringArray(id))); 
      } else { 
       // something wrong with the XML, don't add anything 
      } 
     } 
     ta.recycle(); 

Exam類是一個簡單的數據holder類:

public class Exam { 
    String name, desc, date; 

    public Exam(String[] dataArray) { 
     this.name = dataArray[0]; 
     this.desc = dataArray[1]; 
     this.date = dataArray[2]; 
    } 
} 

然後你會在自定義ADA使用extractedDataArrayList與您的行佈局:

public class CustomAdapter extends ArrayAdapter<Exam> { 

    private LayoutInflater mInflater; 

    public CustomAdapter(Context context, int textViewResourceId, 
      List<Exam> objects) { 
     super(context, textViewResourceId, objects); 
     mInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = mInflater.inflate(
        R.layout.your_layout_file, parent, false); 
     } 
     Exam e = getItem(position); 
     ((TextView) convertView.findViewById(R.id.name)).setText(e.name); 
     ((TextView) convertView.findViewById(R.id.desc)).setText(e.desc); 
     ((TextView) convertView.findViewById(R.id.date)).setText(e.date); 
     return convertView; 
    } 

} 
+0

謝謝!我必須去,但是當我回來時,我會閱讀它並評論是否有什麼不明白的東西。非常感謝! – 2012-07-29 12:41:46

+0

我有一個問題 - CustomAdapter是什麼?我應該把它作爲一個新班級加入嗎?如果是的話 - 如何在主類和CustomAdapter之間進行連接? – 2012-07-30 11:36:31

+1

@RoniCopul在Android中代表數據列表的'ListView'需要一個'Adapter'類型的對象,它將提供數據和行視圖放在屏幕上。我剛製作了一個自定義適配器,以便您可以修改默認適配器提供數據的方式。'custom adapter android'後的谷歌搜索應該提供給你所需要的所有信息(這裏是一個教程http://www.vogella.com/articles/AndroidListView/article.html的例子)。 – Luksprog 2012-07-30 12:50:26

0

我最近學會了如何通過下面這個教程

http://developer.android.com/training/basics/network-ops/xml.html

希望它來解析在Android的XML幫助:)

+0

謝謝,但它也有用的名單?或者,也許我錯了,使用結構XML文件將數據從XML文件移動到應用程序有不同的方法嗎? – 2012-07-28 21:32:17

+0

您是否已經在應用程序中列出了所有列表的值,或者是否需要從某處取回它們? – Meatje 2012-07-28 21:42:46

+0

我有一個XML文件,其中包含列表中項目的結構以及一個包含所有信息的XML文件,但我不知道如何獲取信息並將其作爲列表顯示在屏幕上項目是按照它們寫入XML文件的方式構建的(問題不在於如何使其成爲列表,而是如何獲取數據並使用它) – 2012-07-28 21:49:59