Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make items clickable in list view?

I have been trying to search for a solution, but with very little success. I have to display a pop up window containing a list of items. I was able to display the window but onitemclicklistener is not being called upon clicking an item in the list view. Any help with this issue would be greatly appreciated.

Thanks

Edit1:

public class PopUpWindowActivity extends Activity {

    /** Called when the activity is first created. */
    String[] countries = new String[] {
        "India", "USA", "Canada"
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ListView lv = new ListView(this);
        lv.setAdapter(new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, countries));
        lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView <? > arg0, View arg1, int arg2,
                    long arg3) {
                    Log.v("clicked", (String)((TextView) arg1).getText());
                }

            });
        ll.addView(lv);
        setContentView(ll);
    }
}

In the above code, i tried to create a layout inside which i added a list view. This makes the list view no longer clickable. I have to do this because, i am trying to implement a pop up window inside which there should be multiple items along with a list view.

like image 906
user1253887 Avatar asked Mar 07 '12 06:03

user1253887


People also ask

What is clickable in Android?

clickable - Defines whether this view reacts to click events. Must be a boolean value, either "true" or "false".

What is AdapterView?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.

What is the use of list view?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


2 Answers

Is the list and items in the list set to clickable? Either programmatically...

ListView myList = (ListView) findViewById(R.id.list_view_id);
myList.setClickable(true);

Or in the XML...

   <ListView xmlns:android="http://schemas.android.com/apk/res/android"
       android:clickable="true">
   ...
   </ListView>

I assume you did that, but sometimes we miss even the obvious :)

EDIT:

From the Android Tutorial here is how to set an onItemClickListener programmatically.

 ListView lv = getListView();
 lv.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // When clicked perform some action...
   }
 });

EDIT 2:

Here is my XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list" />

</LinearLayout>

And here is my code

 public class HelloAndroidActivity extends Activity {

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         String[] countries = getResources().getStringArray(R.array.countries_array);

         ListView lv = (ListView) findViewById(R.id.list);
         lv.setAdapter(new ArrayAdapter < String > (this, R.layout.list_item, countries));
         lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 public void onItemClick(AdapterView <? > arg0, View view, int position, long id) {
                     // When clicked, show a toast with the TextView text
                     Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                         Toast.LENGTH_SHORT).show();

                 }

             });
     }
 }
like image 135
Mike Welsh Avatar answered Sep 20 '22 18:09

Mike Welsh


ListView myList;
myList = (ListView)findViewById(list_vew_id_in_xml)
myList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View  view, int position, long id) 
        {
      //Toast.makeText(BritishCouncilActivity.this, "" + position, 2).show();
         }
 });
like image 42
Amey Haldankar Avatar answered Sep 20 '22 18:09

Amey Haldankar