A ListView application in Android

June 13, 2012
By

In this article we are going to display a set of countries in a listview.


1. Creating an Android project namely ListViewDemo

Create New Project

Figure1 : Create new project


2. Select the Android API level of the build target ( API level 14 )

Build target

Figure 2: Select Build Target


3. Enter Application Details :

Application Details

Figure 3: Enter application details


4. Open and update the file /res/layout/main.xml with the given below code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id = "@+id/listview"
    />

</LinearLayout>


5. Open and Update the file /src/in/wptraffianalyzer/listviewdemo/MainActivity.java

package in.wptrafficanalyzer.listviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    String[] countries = new String[] {
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };

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

        // ArrayAdapter is used to store an array of arbitrary objects
        // The second argument to the constructor is a resource id containing a textview for drawing each item in a view
        // android.R.layout.simple_list_item_1 is a built in layout containing textview
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
        ListView listView = (ListView) findViewById(R.id.listview);

        // ArrayAdapter indirectly implements ListAdapter interface
        listView.setAdapter(adapter);
    }
}


6. Run this application in Android Virtual Machine from eclipse ide by clicking the menu Run -> Run

ListView Demo

Figure 4: Application in execution


  7. Download the full application source code


8. Testing with Android Testing Framework

Only the 50% of the application development is completed now. The remaining 50% is with the testing of the application and it is discussed in the article ¬†titled “Android Testing Framework – Testing ListView” .


9. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags:

One Response to A ListView application in Android

  1. camil on May 8, 2015 at 5:00 am

    hi George, excellent work!!!…please i need your help, I am trying to create an application to study a type application test with SQLite database, but I want the answers to be displayed in a listview and not with buttons or radio buttons, please, is the I appreciate very much!!!

Leave a Reply to camil Cancel reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner