• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 30 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

30 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

  22. gowsalya says:

    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

  23. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website. full stack developer training in tambaram

    full stack developer training in velachery



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

  25. Unknown says:

    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

  26. Unknown says:

    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

  27. sai says:

    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

  28. gowsalya says:

    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

  29. ummi ari says:

    I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
    Blueprism training in Pune

    Blueprism online training

    Blue Prism Training in Pune

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

Leave a Reply