Tuesday, 12 June 2012

Simple ListView Using XML DataSource (Part 1)

Hello Friends,

 

Today we have discussed about the simple listview. In this listview load contents from xml resources.

 

If  you are new for android development. You must know about the

develop listview. And this are the basic tutorials for simple listview.

 

Two main types of listview in android, Simple Listview and Custom Listview. 


Here I explain this simple listview by adding listview widget in XML file. You can also use listview in your project by extending ListActivity.

 

There are 3 ways to load data in simple listview.

        1). Load data using XML.
        2). Load data using String Array.
        3). Load data using ArrayList.

 

The following picture shows output of our Android program.

 

 

This simple listview’s code given below.

   

1). First create a new Android project 

For more information, see Create an Android Project with Eclipse

 

2). Create the following Java class 

Main.java

package com.gami.simplelistview;


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


public class Main extends Activity {


        public ListView mylistview;
        public ArrayAdapter<CharSequence> listAdapter;
   
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.main);  
               mylistview = (ListView)findViewById(R.id.listView1);
       
               listAdapter =ArrayAdapter.createFromResource(this,

               R.array.week_days,R.layout.simplerow);      
               mylistview.setAdapter(listAdapter);

               

              //for Listview Item Click.

               mylistview.setOnItemClickListener(new OnItemClickListener() {
               @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                   long arg3) {
                     String s = listAdapter.getItem(position).toString();
                     Toast.makeText(Main.this, "Selected Day is " + s, 3000).show();
                 }
               });
        }
}

 

3). Create the following 2 layouts

main.xml

<?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:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
</LinearLayout>

   

simplerow.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

 

4).  Add listview items in ..res/values/strings.xml

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Simple ListView Using XML</string>


    <string-array name="week_days">
        <item>Sunday</item>
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
    </string-array>
</resources>

No comments:

Post a Comment