Android ListView OnClick Example - ANDROID - Helper

Monday, April 11, 2011

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://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

33 comments:

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

    ReplyDelete
  2. 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]));
    }
    });

    }
    }

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

    ReplyDelete
  4. 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();
    }
    };

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

    ReplyDelete
  6. 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.

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

    ReplyDelete
  8. HELLO YAR,

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

    ReplyDelete
  9. Hi akila krishnan,

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

    Thanks.

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

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

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

    ReplyDelete
  13. what is android.R.Layout.simple_list_item_1? what does it contain?

    ReplyDelete
  14. 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.

    ReplyDelete
  15. 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

    ReplyDelete
  16. 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!

    ReplyDelete
  17. 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......

    ReplyDelete
  18. 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

    ReplyDelete
  19. 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

    ReplyDelete
  20. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    digital marketing training in marathahalli

    digital marketing training in rajajinagar

    Digital Marketing Training in online


    full stack developer training in pune


    full stack developer training in annanagar

    ReplyDelete
  21. Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.
    Click here:
    python training in OMR
    Click here:
    python training in Bangalore

    ReplyDelete
  22. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  23. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Python training in marathahalli
    AWS Training in chennai

    AWS Training in bangalore

    ReplyDelete
  24. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai

    Devops Training in Bangalore

    ReplyDelete
  25. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

    ReplyDelete
  26. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    java training in annanagar | java training in chennai

    java training in chennai | java training in electronic city

    ReplyDelete
  27. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Training in Toronto | Amazon Web Services Training in Toronto , Canada

    AWS Training in New york city | Amazon Web Services in New York City


    AWS Training in London | Amazon Web Services Training in London , UK

    ReplyDelete
  28. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    advanced excel training in bangalore

    ReplyDelete