Thursday, July 12, 2012

ListView in Android



Click Here to download source code

Package Name   :  selva.list

Project Name     :  ListView 

Version              :  1.5 ( Supports 1.5 and above versions)

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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>




ListViewActivity.java


package selva.list;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewActivity extends ListActivity
{
    String[] Fruits = {
            "Apple",
            "Banana",
            "Orange",
            "Mango",
            "Grapes",
            "Jack Fruit",
            "Strawberry",
            "cucumber",
            "pumpkin"
        };
   
    /** Called when the activity is first created. */
    @Override
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, Fruits));
    }
public void onListItemClick(ListView parent, View v, int position, long id)
    {
    Toast.makeText(this,"You have selected " + Fruits[position],Toast.LENGTH_SHORT).show();
    }
}




OUTPUT : 











































Click item   "Apple"



































































































































Click Here to download source code


No comments:

Post a Comment