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!

Advertisements

, , , , , , , , ,

  1. #1 by harika on July 27, 2012 - 6:26 pm

    thanks for the list view tutorials
    check the app we just launched https://play.google.com/store/apps/details?id=com.smartappgenerator.dailyquiz using list view

    • #2 by Deepthi G on August 2, 2012 - 6:52 pm

      Hey Harika,
      The app is superb.. Keep Reading..:)

      • #3 by Mehmet on May 25, 2014 - 7:36 am

        Türkçe biliyor musun?

  2. #4 by venky on July 30, 2012 - 2:55 pm

    I create simple app i.e filled name and phone number . and mini maze app after i opened web-browser . my requirement is after open the browser i want to display name and phone number what did i write name and phone number in my simple app ?

    please tell me

    thank you .

  3. #5 by Bayanaa on December 6, 2012 - 6:28 am

    Hello there,
    Can you see this question and please help me>
    http://stackoverflow.com/questions/13467645/how-to-insert-list-xml-file-inside-main-xml

  4. #6 by Debora on May 31, 2013 - 1:37 pm

    Everything is very open with a really clear description of the challenges.
    It was really informative. Your site is extremely helpful. Thank you
    for sharing!

  5. #7 by android application on August 20, 2014 - 11:11 am

    Its not my first time to visit this website, i am
    visiting this website dailly and obtain fastidious facts from here daily.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: