ListView with ListActivity in Android

June 20, 2012
By

In this article, we are creating a listview widget using ListActivity. In this demo, we are not using any layoutfile instead we make use the default layout of the listview.


1. Create an Android project namely ListViewWithListActivity

New Android Project

Figure 1 : New Android Project


2. Select a build target for this application

Select build target

Figure 2: Select build target


 

3. Enter application details

Application Details

Figure 3 : Application Details


4. Open and edit the file /src/in/wptrafficanalyzer/listviewwithlistactivity/MainActivity.java


package in.wptrafficanalyzer.listviewwithlistactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {

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

        // Declaring ArrayAdapter for the default listview
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countries);

        // Setting ArrayAdapter for the default listview
        setListAdapter(adapter);

        // Defining ItemClick event Listener
        OnItemClickListener listener = new OnItemClickListener() {

            // This method will be triggered when an item in the listview is clicked ( or touched )
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
                Toast.makeText(getBaseContext(), "You selected : " + countries[position], Toast.LENGTH_SHORT).show();
            }
        };

        // Setting an ItemClickEvent Listener for the listview
        // In this example we are making use the default listview
        getListView().setOnItemClickListener(listener);
    }
}


 

5. Run the application

Application in Execution

Figure 4: Application in Execution


6. Download the source code


7. 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: ,

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner