3

Let's say that I have a ListView with some items. I want to make it so when the user clicks on an item, the app pops up a Toast containing the name of the item. For example, when the user clicks "Apple", they are presented with the toast: "You ate Apple." How can I do so?

4 Answers 4

1

Standard way it to use .setOnItemClickListener().

Sign up to request clarification or add additional context in comments.

1 Comment

But how to get the name of the pressed item?
0

you can set Item Click Listener on List View i.e.

listvw=(ListView)findViewById(R.id.listviewid);

listvw.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

            Toast.makeText(ActivityName.this, "Text message", Toast.LENGTH_SHORT).show();

        }
    });

3 Comments

Same here. How to get the name of the pressed item?
You have a view of your listview item. just find the text of view i.e. ' TextView text= (TextView)view.findViewById(R.id.text_viewId); ' 'Toast.makeText(ActivityName.this, text.getText(), Toast.LENGTH_SHORT).show();'
OK thanks. I used TextView text = (TextView) arg1.findViewById(arg1.getId()); Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH.SHORT).show();
0

use like that

ActivityListView .java

package com.sunil;  

    import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.Toast; 

    public class ActivityListView extends ListActivity {

     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Create an array of Strings, that will be put to our ListActivity 
     String[] namesArray = new String[] { "Linux", "Windows7", "Eclipse", 
        "Suse", "Ubuntu", "Solaris", "Android", "iPhone" }; 

     /* Create an ArrayAdapter, that will actually make the Strings above
      appear in the ListView */ 

     this.setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, namesArray)); 
     } 
      @Override 
      protected void onListItemClick(ListView l, View v,  
      int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     // Get the item that was clicked 

     Object o = this.getListAdapter().getItem(position); 
     String keyword = o.toString(); 
     Toast.makeText(this, "You selected: " + keyword,  
       Toast.LENGTH_SHORT).show(); 
     } 
    } 

Main.xml

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout 

     xmlns:android="http://schemas.android.com/apk/res/android" 

     android:orientation="vertical" 

     android:layout_width="fill_parent" 

     android:layout_height="fill_parent"> 

     <TextView 

      android:layout_width="fill_parent" 

      android:layout_height="wrap_content" 

      android:text="@string/hello" /> 

    </LinearLayout> 

2 Comments

I get a java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'. Maybe let's extend the standard Activity class, not ListActivity?
same code working in my device. Do not extends Activity and also setcontentview also . It will work fine check again
0

Try this code listview=(ListView)findViewById(R.id.listid);

    listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {

       // to set your list item or name  

        //here list is your list that you set in your adapter       
          list.get(pos);

    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.