• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 21 comments


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://lh6.googleusercontent.com/_xnrF0YSUul8/TaPcM8-D-TI/AAAAAAAAAGo/9uyK_oFKYXs/listview_onclick1.png

https://lh5.googleusercontent.com/_xnrF0YSUul8/TaPdt6GSm1I/AAAAAAAAAG4/NNcHKvX1N-M/listview_onclick2.png

21 Responses so far.

  1. Darshan says:

    HOw to pass data from one lisview to another listview like if i select country i will be get state of that in listview

  2. Android says:

    Hi Dharsan,

    I think this may help you.

    package com.TwoListView;

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

    public class TwoListView extends Activity {

    String listview_array1[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE" };

    String listview_array2[][] = { { "11", "12" }, { "21", "22", "23" },
    { "31", "32" }, { "41", "42", "43" }, { "51", "52" } };

    ListView lv1, lv2;

    int selected = 0;

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lv1 = (ListView) findViewById(R.id.listView1);
    lv2 = (ListView) findViewById(R.id.listView2);

    lv1.setAdapter(new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, listview_array1));

    lv1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView arg0, View v, int position,
    long id) {
    selected = position;

    lv2.setAdapter(new ArrayAdapter(TwoListView.this,
    android.R.layout.simple_list_item_1,
    listview_array2[selected]));
    }
    });

    }
    }

  3. diehard says:

    after long search i get right tutorial
    great work.....

  4. abhi019 says:

    it helped grt wrk :)

  5. Thanks! I was trying OnClick handler all night and this publication help me so much, am trying to show something to verify that the OnItemClick was working but no matters because i use a TOAST to show "some text". Good, am change for yours:

    ListViewClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
    //Acciones necesarias al hacer click
    //Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
    // Toast.LENGTH_LONG);
    Object o = serverList.getItemAtPosition(position);
    AlertDialog.Builder adb = new AlertDialog.Builder(
    DroidKinesisActivity.this);
    adb.setTitle("ListView OnClick");
    adb.setMessage("Selected Item is = "
    + o);
    adb.setPositiveButton("Ok", null);
    adb.show();
    }
    };

  6. how to make a two xml files for 2listview?

  7. can you tell me how to show text after selecting an item from listview.
    for example:
    if i click on "three" then it will show me "three comes after two and before four" as scroll-able text.
    please provide full code.
    thank you in advance.

  8. Hannah says:

    Thank you so much for this. Simple and easy to understand compared to other tutorials I've found. Excellent job. :)

  9. HELLO YAR,

    I'm searching for a code ie, listview onitemclick must go to a webpage pls help me with this thanks yar

  10. Jack says:

    Hi akila krishnan,

    You may use webview on listview onitemclick. load the url you wants to display on the webview.

    Thanks.

  11. nice thankuuuuuuuuuuuuuuuuuu

  12. How to change color of ListView items on focus and on click??

  13. Krish says:

    Hey Really Thanks man...It works perfectly.............

  14. rk says:

    hi
    i want to create an application that when i click on listview item,it dials the phone number.Thanks in advance

  15. what is android.R.Layout.simple_list_item_1? what does it contain?

  16. thankyou so much sir..i was trying to solve a program from a month i was not able to get help from anyone on my problem.Your logic gave all answers and my app is complete.

  17. Really can you solve problems practically and staff in a well explained. I wanted to ask if you can help in the following fizes as I saw the look, and I like yours and is very good, but now want to load on one and he send me to a new window.

    Thank You

  18. Admin says:

    How Do i link the Rows ONE,TWO,THREE,FOUR and FIVE with Activities
    for instance, if "ONE" is clicked , user will be taken to Another activity named Activity_one. ( i DONT want any toast to appear in my listview!)
    Thank You!

  19. Hey, it would be a great help if someone can tell how to create the PDF of an list view item on the onclick of item......

  20. hi i am creating an application in which i am using listview. when i click on listview item next page should be open.any one can help me please

  21. Saranya N says:

    Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
    Android Training|Android Training in chennai with placement | Android Training in velachery

Leave a Reply