Archive for June, 2012

A Simple ListView in Android

Using a ListView class we can create a scrollable list of items. Here I would discuss about a simple ListView that displays a list of strings which I’ve stored in the strings.xml file.

Let’s see the strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyListViewActivity!</string>
 <string name="app_name">MyListView</string>
 <string-array name="strings">
 <item >Android</item>
 <item >Android Desk</item>
 <item >Android Desk by Deepthi</item>
 <item >Posts by Deepthi</item>
 <item >Android Tutorials</item>
 </string-array>
</resources>

The main.xml file is shown below:

<?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" >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello" />
</LinearLayout>

As you know an ArrayAdapter can handle data based on Arrays. The data is provided to the ListView through an ArrayAdapter and the ArrayAdapter is set as the ListAdapter.

Just look at the code:

ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.strings));
 setListAdapter(myAdapter);

This enables the list of data to be displayed as shown in the figure below:

Now let’s display a Toast telling what we have clicked in the list of data as we click on each item in the List.

The code:

@Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
 // TODO Auto-generated method stub
 super.onListItemClick(l, v, position, id);
 String item=(String)getListAdapter().getItem(position);
 Toast.makeText(getApplicationContext(),item+" Selected!!", Toast.LENGTH_LONG).show();
 }

Just look at the figure below.

The Whole code:

package com.deepthi.mylistview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListViewActivity extends ListActivity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.strings));
 setListAdapter(myAdapter);
 
 }
 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
 // TODO Auto-generated method stub
 super.onListItemClick(l, v, position, id);
 String item=(String)getListAdapter().getItem(position);
 Toast.makeText(getApplicationContext(),item+" Selected!!", Toast.LENGTH_LONG).show();
 }
}

Here I’ve created a subclass of ListActivity which is indeed the subclass of the class Activity. The ListActivity makes the handling of Listview easy to handle.

Hope this post was useful. Keep Reading!

, , , , , , , , ,

7 Comments