【问题标题】:XML Parsing (Moving data from XML to a list)XML 解析(将数据从 XML 移动到列表中)
【发布时间】:2012-07-28 21:20:42
【问题描述】:

我正在构建一个包含列表的应用程序,列表中的每个项目都有一些值(名称、描述和日期)。我制作了一个 XML 文件,其中包含列表中任何项目的结构。
我还有另一个 XML 文件,其中包含列表中的项目(每个 <item> 标签都有 <name><desc><date> 孩子)
问题是我不知道怎么把所有东西放在正确的地方。。我在网上搜索过,发现它叫XML Parsing,但我找到的唯一教程是this one,但写得不清楚所以没看懂。。
有人可以解释一下或给我一个很好的教程吗?

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

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您设置为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 类是一个简单的数据持有者类:

    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 与您的行布局:

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

    【讨论】:

    • 谢谢!我得走了,但是当我回来时,如果有什么不明白的地方,我会阅读并发表评论。非常感谢!
    • @RoniCopul ListView 在 android 中表示数据列表,需要一个 Adapter 类型的对象,它将提供数据和要放在屏幕上的行视图。我刚刚制作了一个自定义适配器,因此您可以修改默认适配器提供数据的方式。在custom adapter android 之后进行谷歌搜索应该会为您提供所需的所有信息(这里是教程vogella.com/articles/AndroidListView/article.html 的示例)。
    • @RoniCopul 您所要做的就是在活动的onCreate 方法中创建一个CustomAdapter(CustomAdapter adapter = new CustomAdapter(this, 0, extractedData);) 类型的对象,并将其设置为ListViewlistView.setAdapter(adapter);。就是这样。
    • @RoniCopul 在getView 方法中,你为ListView 构建View row 所以你撅嘴一个项目的布局文件,你发布的那个在问题中。
    • @RoniCopul 检查ListView 的文档并在onCreate 方法中简单地使用setListAdapter(adapter); 方法。
    【解决方案2】:

    我最近通过本教程学习了如何在 android 中解析 XML

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

    希望对你有帮助:)

    【讨论】:

    • 谢谢,但它对列表也有用吗?或者也许我错了,有一种不同的方法可以使用结构 XML 文件将数据从 XML 文件移动到应用程序?
    • 您是否已经在应用程序中拥有列表的所有值,还是需要从某个地方检索它们?
    • 我有一个包含列表中项目结构的 XML 文件和一个包含所有信息的 XML 文件,但我不知道如何获取信息并将其显示在屏幕作为列表,当每个项目的结构都以它们的方式写入 XML 文件时(问题不是如何使其成为列表,而是如何获取数据并使用它)
    • 我认为将两个 XML 文件的(部分)添加到 OP 中可能是个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2020-06-03
    • 2017-06-20
    • 2014-12-25
    • 1970-01-01
    • 2021-11-04
    相关资源
    最近更新 更多