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

2 Answers 2

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; } } 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks! I have to go but when I'm back I'll read it and comment if something isn't understood.. Thanks a lot!
@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).
@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.
@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.
@RoniCopul Check the documentation of a ListView and simply use the method setListAdapter(adapter); in the onCreate method.
|
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 :)

4 Comments

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?
Do you have all the values for the list already in the application or do you need to retrieve them from somewhere?
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)
I think it might be a good idea to add (parts) of both XML files to the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.