Wednesday, December 26, 2012

Android ListView OnClick Example


LISTVIEW ONCLICK
 
SOURCE CODE [main.xml] is 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"
android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android">
                <ListView android:id="@+id/listview"
android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />
</LinearLayout>

SOURCE CODE [ListviewOnclickExample.java] is 

package com.ListViewExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListviewOnclickExample extends Activity
{
                private ListView lv;
                private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
                                                "SEVEN", "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle icicle)

{
super.onCreate(icicle);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
AlertDialog.Builder adb = new AlertDialog.Builder(
ListviewOnclickExample.this);
adb.setTitle("ListView OnClick");
adb.setMessage("Selected Item is = "
+ lv.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();                     
                                                }
                                });
                }
}


The OUTPUT will be

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwdR8Tu2YHGYnokTE82Mt2FD_4nRxUEUZTn00O86so5Vsz1pocChjZHVykwyol5NfwFNF_r5BdHHjhx9N98M7D3aeTcIfWg9SHdqJNJj0ODQ3X8sLXyFheSZXzsHcJ6HjKdiivIdJoNU4/

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiX5AhCUf0BFXcc_ue3lvsdyX86o_-mkZMO_VFB0PuqtKBa5WkFji8tHy2zas6AH5RboYT-Snk__72SdRV1sWYv5__0C4gQvT8vz-Ns0BuCe3cMwCzZQsTRZiWSDnRq9Wl4kPvs5LIv-7E/

No comments:

Post a Comment

Popular Posts