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://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhYzCpunz9zQ6nNM_-NVnHqpCUR0EeXAb_JwdpE8XjEzXHOt-wR_L4A9pRiU0tMCtI4BlkMh8DXvOkyTR9DIZogduzr2zCWeIuVqupCaBqOOLuca8fyWe4REuMIgrVhjFlkPnJ7fab5ok/

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFkLPvSe1jF8S3-DdVH_wCfeJA0sEX9j79K4MrkZwCwKrVImukoBZdzCk3-xYhNENI6RE-GlSn1hDUknqqqTAr_4vYp1CkuF-dRdjRwI8Bq2hkDnf6XcmJrM_vBoBTTbMqx2oUJh1RUAY/

119 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. it helped grt wrk :)

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

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

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

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

    ReplyDelete
  9. HELLO YAR,

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

    ReplyDelete
  10. Hi akila krishnan,

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

    Thanks.

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

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

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

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

    ReplyDelete
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  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. Really nice and useful post. Thanks for sharing his kind of information, Keep posting.

    Data Science

    ReplyDelete
  29. You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!!
    data analytics certification courses in Bangalore

    ReplyDelete
  30. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete



  31. This post is very simple to read and appreciate without leaving any details out. Great work!

    DATA SCIENCE COURSE MALAYSIA

    ReplyDelete
  32. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!data science course in dubai

    ReplyDelete
  33. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Data Science courses in Bangalore

    ReplyDelete

  34. Really appreciate this wonderful post that you have provided for us.Great site and a great topic as well i really get amazed to read this. Its really good.
    www.technewworld.in
    How to Start A blog 2019
    Eid AL ADHA

    ReplyDelete
  35. I love your article so much. Good job
    Participants who complete the assignments and projects will get the eligibility to take the online exam. Thorough preparation is required by the participants to crack the exam. ExcelR's faculty will do the necessary handholding. Mock papers and practice tests will be provided to the eligible participants which help them to successfully clear the examination.

    Excelr Solutions

    ReplyDelete
  36. Thanks a lot for writting such a great Blog. It's really has lots of insights and valueable informtion. I really appreciate, Now i am waiting for your next Blog!!

    artificial intelligence course

    ReplyDelete
  37. Attend The Data Analytics Course Bangalore From ExcelR. Practical Data Analytics Course Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course Bangalore.
    ExcelR Data Analytics Course Bangalore

    ReplyDelete
  38. I learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from python training in btm layout experts who know advanced concepts which can help to solve any type of Real-time issues in the field of Python. Really worth trying hkbk group of institutions

    ReplyDelete
  39. Thanks for sharing Great info… learning driving from experienced instructors help you to learn driving very fast. Driving School Melbourne

    ReplyDelete
  40. Excellent information with unique content and it is very useful to know about the Selenium.Selenium training in bangalore

    ReplyDelete
  41. Phishing, social networking and protection of data are the steps involved in cyber security course

    ReplyDelete
  42. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.aws training in bangalore

    ReplyDelete
  43. There are various steps in business intelligence to analyze the business. learn on business intelligence through msbi online training

    ReplyDelete
  44. Your topic is very nice and helpful to us … Thank you for the information you wrote.

    Bangalore Training Academy located in Bangalore, is one of the best Workday Training institute with 100% Placement support. Workday Training in Bangalore provided by Workday Certified Experts and real-time Working Professionals with handful years of experience in real time Workday Projects.

    ReplyDelete
  45. There is certainly a lot to find out about this subject. I really like all the points you made. onsite mobile repair bangalore You made some really good points there. I checked on the net for additional information about the issue and found most individuals will go along with your views on this web site. asus display repair bangalore Good post. I learn something new and challenging on websites I stumbleupon on a daily basis. It's always useful to read content from other authors and use a little something from other web sites. huawei display repair bangalore

    ReplyDelete
  46. I blog often and I seriously appreciate your information. The article has truly peaked my interest. I am going to take a note of your website and keep checking for new details about once per week. I subscribed to your RSS feed as well. online laptop repair center bangalore Pretty! This was a really wonderful post. Thank you for supplying this information. dell repair center bangalore

    ReplyDelete
  47. Greetings! Very helpful advice within this article! It's the little changes that make the most significant changes. Many thanks for sharing! acer repair center bangalore Howdy! This article couldn’t be written much better! Looking through this article reminds me of my previous roommate! He constantly kept talking about this. I will send this article to him. Fairly certain he's going to have a good read. Thank you for sharing! macbook repair center bangalore

    ReplyDelete
  48. Thank you for sharing such a nice post!

    Upgrade your career Learn DevOps Training from industry experts gets complete hands on Training, Interview preparation, and Job Assistance at My Training Bangalore.

    ReplyDelete
  49. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Looking for SAP BASIS Training in Bangalore, learn from eTechno Soft Solutions SAP BASIS Training on online training and classroom training. Join today!

    ReplyDelete

  50. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work!
    data analytics course in bangalore
    data science interview questions

    ReplyDelete
  51. Sharing information are really valuable. if you any issue for Pizza Oven then Visit us at Wisdoms Oven Repair

    ReplyDelete
  52. This is so elegant and logical and clearly explained. Brilliantly goes through what could be a complex process and makes it obvious...

    cyber security training online

    ReplyDelete
  53. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science training in Hyderabad

    ReplyDelete
  54. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science training in Hyderabad

    ReplyDelete
  55. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing.. microsoft azure tutorial

    ReplyDelete
  56. A very nice guide. Your blog is really nice and informative. Thank you for sharing such detailed article. I am learning a lot from you.
    AI Training in Hyderabad

    ReplyDelete
  57. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    best data analytics courses in mumbai

    ReplyDelete
  58. Expected to form you an almost no word to thank you once more with respect to the decent recommendations you've contributed here.
    Machine Learning Training In Hyderabad

    ReplyDelete
  59. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science courses

    ReplyDelete
  60. I have to agree with the valid points you make in your article because I see things like you. Additionally, your content is interesting and really good reading material. Thank you for sharing your talent.
    Best Data Science training in Mumbai

    Data Science training in Mumbai


    ReplyDelete
  61. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    data science interview questions

    ReplyDelete
  62. It is an informative blog. I would like to know more information about the new model bike. Anyway thanks a lot for sharing this post.Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervour like mine to grasp great deal more around this condition.



    Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course


    ReplyDelete
  63. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly.Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly
    Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  64. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly
    Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  65. Hi! Great post. Thank you for sharing a big amount of valuable information with us Keep Sharing.

    Data Science training institute in Hyderabad

    Data Science training in Hyderabad

    ReplyDelete
  66. I would you like to say thank you so much for my heart. Really amazing and impressive post you have the share. Please keep sharing

    Python Training in Hyderabad

    Best PythonTraining Institutes in Hyderabad

    ReplyDelete
  67. Great blog!!! It is very impressive... thanks for sharing with us...keep posting.

    Python Training in Hyderabad

    Best PythonTraining Institutes in Hyderabad

    ReplyDelete
  68. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  69. Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.This is incredible,I feel really happy to have seen your webpage.I gained many unknown information, the way you have clearly explained is really fantastic.keep posting such useful information.
    Full Stack Training in Chennai | Certification | Online Training Course
    Full Stack Training in Bangalore | Certification | Online Training Course

    Full Stack Training in Hyderabad | Certification | Online Training Course
    Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai
    Full Stack Training

    Full Stack Online Training



    ReplyDelete
  70. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    Simple Linear Regression

    Correlation vs Covariance

    Simple Linear Regression

    Correlation vs covariance

    KNN Algorithm

    ReplyDelete
  71. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  72. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.

    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training

    ReplyDelete
  73. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..

    Data Science Training in Hyderabad

    ReplyDelete
  74. seogroupbuykit.com is best indian selling for ahref, domcop, spamzilla and semrush.

    ReplyDelete


  75. Nice article and thanks for sharing with us. Its very informative


    Plots in THIMMAPUR

    ReplyDelete
  76. You should search for 2nd hand android phones for sale in various classified ads platforms in your area or Quikads. By this, you could fill up your needs.

    ReplyDelete
  77. So informative & full with good resource.
    The explanation is comprehensive and informative.
    selling sites in Bangladesh

    ReplyDelete
  78. You need to participate in a challenge for probably the best webpage on the web. I will enthusiastically suggest this site!
    evrmag Primavera Online Training | Primavera Course in Chennai

    ReplyDelete
  79. Nice blog and absolutely outstanding. You can do something much better but I still say this perfect.Keep trying for the best.
    business analytics training in hyderabad


    ReplyDelete
  80. Vray Sketchup Crack is a 3D graphic and also plug-in issue that have many kinds of powerful image rendering features. This software application .RekordBox Crack

    ReplyDelete
  81. The sample code and explanations make it so much easier to understand the concept. Thanks for sharing this valuable knowledge.

    Digital marketing courses in illinois

    ReplyDelete
  82. Very interesting to read this article. I have learned some new information. thanks for sharing. well done.
    Digital marketing courses in city of Westminster

    ReplyDelete
  83. Author has explained the concept in easy to understand language. My best wishes to author for such a wonderful post.
    Investment banking courses after 12th

    ReplyDelete