简体   繁体   English

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

[英]XML Parsing (Moving data from XML to a list)

I have am building an app that contains a list, and every item in the list has some values(name, description and date). 我正在构建一个包含列表的应用程序,列表中的每个项目都有一些值(名称,描述和日期)。 I've made an XML file that contains the structure of any item in the list. 我制作了一个XML文件,其中包含列表中任何项目的结构。
I also got another XML file, that contains the items in the list (every <item> tag has <name> , <desc> and <date> children) 我还有另一个XML文件,其中包含列表中的项目(每个<item>标签都有<name><desc><date> children)
The problem is I don't know how to put everything in the right places.. I've searched it in the web and I found that it's called XML Parsing, but the only tutorial I found was this one , but it's written unclearly so I didn't understand it.. 问题是我不知道如何将所有内容放在正确的位置..我在网上搜索过它,我发现它叫做XML解析,但是我发现的唯一一个教程就是这个 ,但它写得不清楚所以我不明白..
Can someone explain it or give me a good tutorial? 有人可以解释一下或给我一个很好的教程吗?

Edit: 编辑:
This is the structure XML file 这是结构XML文件
This is the content XML file 这是内容XML文件

The data that you setup as a string-array is not properly built to be shown in a ListView . 您设置为string-array数据未正确构建,无法在ListView It should be like this: 它应该是这样的:

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

To parse this in a data structure good for a ListView you would write(part of the code comes from this answer: Android Resource - Array of Arrays ): 要在适合ListView的数据结构中解析这个问题,你可以编写(部分代码来自这个答案: Android资源 - 数组数组 ):

 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();

The Exam class is a simple data holder class: Exam类是一个简单的数据持有者类:

public class Exam {
    String name, desc, date;

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

Then you would use the extractedData ArrayList in a custom adapter with your row layout: 然后,您将在具有行布局的自定义适配器中使用extractedData ArrayList

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

}

I recently learned how to parse XML in android by following this tutorial 我最近学习了如何通过本教程解析android中的XML

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

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM