ListView Example 2 – Using Xml Resource File

This is the second blog post in series of posts that will demonstrate how Android ListView can be used in your application. We started the series off with the most basic loading of hard coded arraylist in a ListView. In this post we will create an Xml resource file and load the data in the ListView from that Xml file at runtime.

Lets jump into the second example.

Skill Assumptions:

  1. You have configured your Android development environment on Eclipse or other tools
  2. You know how to create a new Android project

Step 1 – Create a new Eclipse project

Step 2 - Add a default Activity named Start which will add main.xml under layout folder and Start.java under your namespace folder under src.

Step 3 – In your eclipse project under the res folder create a new subfolder named xml [all lowercase]. Create a new Xml file named todolist.xml in your res/xml folder using your favorite editor with following content pasted in it. Save the file and refresh your res folder in Eclipse.

Here is how your folder structure will look like:

// layout/todolist.xml




	
	
	
	

Step 4 – Copy paste following Xml on your main.xml file.

// layout/main.xml






			







		


Step 5 – Copy paste following code in your AndroidManifest.xml file. The most important attribute is to android:theme=”@android:style/Theme.NoTitleBar” which will disable the default Android title bar because we are replacing that with our own Action bar.

// AndroidManifest.xml



    
        
            
                
                
            
        

    
    


Step 6 – Copy and paste following code in your Start.java class file. Notice the resource id – android.R.layout.simple_list_item_1. That is the magic default Android layout that allows us to fill up the ListView without specifying the layout Xml for individual list items.

// src/Start.java

package com.appfulcrum.blog.examples.listviewxml;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.*;
import android.content.res.XmlResourceParser;

public class Start extends Activity {
	private String[] lv_arr = {};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Prepare an ArrayList of todo items
		ArrayList listTODO = PrepareListFromXml();

		// Get a handle to the list view
		ListView lv = (ListView) findViewById(R.id.ListView01);

		// Bind the data with the list
		lv_arr = (String[]) listTODO.toArray(new String[0]);
		lv.setAdapter(new ArrayAdapter(Start.this,
				android.R.layout.simple_list_item_1, lv_arr));

	}

	public ArrayList PrepareListFromXml() {
		ArrayList todoItems = new ArrayList();
		XmlResourceParser todolistXml = getResources().getXml(R.xml.todolist);

		int eventType = -1;
		while (eventType != XmlResourceParser.END_DOCUMENT) {
			if (eventType == XmlResourceParser.START_TAG) {

				String strNode = todolistXml.getName();
				if (strNode.equals("item")) {
					todoItems.add(todolistXml.getAttributeValue(null, "title"));
				}
			}

			try {
				eventType = todolistXml.next();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return todoItems;
	}
}

Step 6 – Run the application as Android Application and you should see below for output.

Tags: , , , ,

http://downloadpart.com