Wednesday 17 October 2012

ANDROID - ListView Adding Listeners

Demo:

1)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:background="#5F04B4"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>



2)Activity:

package com.venky.listviewaddinglisteners;

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;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class ListViewaddingListenersActivity extends Activity {
      /** Called when the activity is first created. */

      ListView listview;

      String[] places;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            places = getResources().getStringArray(R.array.places);

            // call References
            listview = (ListView) findViewById(R.id.listView1);

            // Creating ArrayAdapter
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                        ListViewaddingListenersActivity.this,
                        android.R.layout.simple_list_item_1, places);

            listview.setAdapter(arrayAdapter);

            // set the Adapter for ListView
            listview.setAdapter(arrayAdapter);

            // set OnItemClickListener
            listview.setOnItemClickListener(new OnItemClickListener() {

                  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                              long arg3) {
                        // TODO Auto-generated method stub

                        Toast.makeText(getApplicationContext(),
                                    "You Clicked => " + places[arg2], Toast.LENGTH_LONG)
                                    .show();
                  }
            });

            // set OnItemLongClickListener
            listview.setOnItemLongClickListener(new OnItemLongClickListener() {

                  public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                              int arg2, long arg3) {
                        // TODO Auto-generated method stub

                        Toast.makeText(getApplicationContext(),
                                    "You Long Clicked => " + places[arg2],
                                    Toast.LENGTH_LONG).show();

                        return false;
                  }
            });

            // set OnItemSelectedListener
            listview.setOnItemSelectedListener(new OnItemSelectedListener() {

                  public void onItemSelected(AdapterView<?> arg0, View arg1,
                              int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(),
                                    "You Selected => " + places[arg2], Toast.LENGTH_LONG)
                                    .show();

                  }

                  public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                  }
            });
      }
}



3)Add this code in your strings.xml file:

<string-array name="places">
            <item>Hyderabad</item>
            <item>Bangalore</item>
            <item>Chennai</item>
            <item>New Delhi</item>
            <item>India</item>
            <item>Srilanka</item>
            <item>USA</item>
            <item>UK</item>
        </string-array>



4)Download this Project Click Here

No comments:

Post a Comment