XML解析(将数据从XML移动到列表)

问题描述:

我正在构建一个包含列表的应用程序,列表中的每个项目都有一些值(名称,说明和日期)。我制作了一个XML文件,其中包含列表中任何项目的结构。
我也有另外一个XML文件,它包含在列表中的项目(每个<item>标签都有<name><desc><date>儿童)
问题是我不知道如何把一切都在正确的地方。我”已经搜索到它的网站和我发现,这就是所谓的XML解析,但我发现的唯一教程this one,但它是写不清楚的,所以我不明白它..
有人能解释它还是给了我一个很好的教程?XML解析(将数据从XML移动到列表)

编辑:
This is结构的XML文件
This is内容的XML文件

您设置为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

我最近学会了如何通过下面这个教程

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