0

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?

Edit:
This is the structure XML file
This is the content XML file

Roni Copul
  • 183
  • 1
  • 1
  • 8

2 Answers2

2

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>

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

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

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

}
Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190
  • 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 Jul 29 '12 at 12:41
  • I have a question - what is the CustomAdapter is? Should I add it as a new class? And if yes - how to I connect between the main class and the CustomAdapter? – Roni Copul Jul 30 '12 at 11:36
  • 1
    @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 http://www.vogella.com/articles/AndroidListView/article.html). – user Jul 30 '12 at 12:50
  • I looked through this tutorial and I'll read it thoroughly, but another question - do I need to add it somewhere or is the fact the adapter extends the ArrayAdapter does the work? And if you still don't understand what I want to ask so the bottom line is - do I need to add something so the adapter will effect the list? – Roni Copul Jul 30 '12 at 13:06
  • 1
    @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` method and set it to the `ListView` with `listView.setAdapter(adapter);`. That is all. – user Jul 30 '12 at 13:14
  • OK thanks, but one more thing (I hope the last one) - in the place written "your_layout_file" should I put the whole page's layout file, or the layout of an item in the list? – Roni Copul Jul 30 '12 at 13:17
  • 1
    @RoniCopul In the `getView` method you build the `View` for a `ListView` **row** so you pout the layout file of an item, the one that you posted in the question. – user Jul 30 '12 at 13:20
  • @Lunksprog THANKS AGAIN, and now another (now I'm pretty sure it's the last one) - setting the adapter to the listView by `listView.setAdapter(adapter);` doesn't work because there isn't object called listView.. My main class extends ListActivity so I tried using "this" instead of "listView" but the method in undefined for the class. – Roni Copul Jul 30 '12 at 13:25
  • 1
    @RoniCopul Check the documentation of a `ListView` and simply use the method `setListAdapter(adapter);` in the `onCreate` method. – user Jul 30 '12 at 13:27
0

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

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

Hope it helps :)

Bart
  • 769
  • 2
  • 13
  • 29
  • 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 Jul 28 '12 at 21:32
  • Do you have all the values for the list already in the application or do you need to retrieve them from somewhere? – Bart Jul 28 '12 at 21:42
  • 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 Jul 28 '12 at 21:49
  • I think it might be a good idea to add (parts) of both XML files to the OP. – Bart Jul 28 '12 at 21:56