Análisis de XML (Mover datos de XML a una lista)

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> la etiqueta tiene <name>, <desc> y <date> niños)
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 esta, but it's written unclearly so I didn't understand it..
Can someone explain it or give me a good tutorial?

Edit:
Es the structure XML file
Es the content XML file

preguntado el 28 de julio de 12 a las 22:07

2 Respuestas

The data that you setup as a string-array is not properly built to be shown in a ListView. Debería ser así:

    <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: Recurso de Android: matriz de matrices ):

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

Los programas 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];
    }
}

Entonces usarías el 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;
    }

}

contestado el 23 de mayo de 17 a las 11:05

Thanks! I have to go but when I'm back I'll read it and comment if something isn't understood.. Thanks a lot! - Roni Copul

@RoniCopul The ListView which, in android, represents a list of data need an Adapter type of object which will supply the data and the rows view to put on the screen. I've just made a custom adapter so you can modify the way the default adapters supply the data. A google search after custom adapter android should supply you with all the information you need(here is an example of tutorial vogella.com/articles/AndroidListView/article.html). - usuario

@RoniCopul All you have to do is create an object of type CustomAdapter(CustomAdapter adapter = new CustomAdapter(this, 0, extractedData);) in your activity's onCreate y configúrelo en el ListView con listView.setAdapter(adapter);. Eso es todo. - usuario

@RoniCopul In the getView method you build the View para agendar una ListView fila so you pout the layout file of an item, the one that you posted in the question. - usuario

@RoniCopul Check the documentation of a ListView and simply use the method setListAdapter(adapter); en la onCreate método. - usuario

I recently learned how to parse XML in android by following this tutorial

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

Espero eso ayude :)

Respondido 28 Jul 12, 22:07

Thanks, but is it useful also for lists? Or maybe I was wrong and there is a different way to move the data from the XML file to the App, using the structure XML file? - Roni Copul

Do you have all the values for the list already in the application or do you need to retrieve them from somewhere? - Bart

I have one XML file that contains the structure of an item in the list, and one XML file that contains all the information, but I don't know how to take the information and show it on the screen as a list, when every item is structured they way it's written in the XML file (The question IS NOT how to make it a list, but how to take the data and use it) - Roni Copul

I think it might be a good idea to add (parts) of both XML files to the OP. - Bart

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.