XML解析(XML从数据移动到列表) [英] XML Parsing (Moving data from XML to a list)

查看:159
本文介绍了XML解析(XML从数据移动到列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说,造一个包含列表中的应用程序,然后在列表中的每个项目都有一定的值(名称,描述和日期)。我做了一个包含列表中的任何项目的结构的XML文件。

我也有另外一个XML文件,它包含在列表中的项目(每个<项目> 标记<名称> <&说明GT; <日期和GT; 子女)

问题是我不知道如何把一切都在正确的地方。我已经搜查它在网上,我发现这就是所谓的XML解析,但我发现的唯一教程的这个,但它的填写不清,所以我不明白它..

有人可以解释它,或者给我一个很好的教程?

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.
I also got another XML file, that contains the items in the list (every <item> tag has <name>, <desc> and <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..
Can someone explain it or give me a good tutorial?

编辑:

这是结构的XML文件

这是内容的XML文件


This is the structure XML file
This is the content XML file

推荐答案

中的数据,你设置为字符串数组未正确建在中显示的ListView 。它应该是这样的:

The data that you setup as a string-array is not properly built to be shown in a 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>

要在数据结构良好的的ListView解析这个你会写(在code的一部分来自这样的回答:的Android资源 - 阵列数组):

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

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

考试类是一个简单的数据holder类:

The Exam class is a simple data holder class:

public class Exam {
    String name, desc, date;

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

然后你可以使用 extractedData 的ArrayList 与您排布置一个自定义适配器:

Then you would use the extractedData ArrayList in a custom adapter with your row layout:

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

}

这篇关于XML解析(XML从数据移动到列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆