ListView in Android, handler for clicking item

Posted by eljainc on Stack Overflow See other posts from Stack Overflow or by eljainc
Published on 2011-02-18T23:22:50Z Indexed on 2011/02/18 23:25 UTC
Read the original article Hit count: 226

Filed under:
|

Hello,

I have an activity in Android which uses a ListView. When I click on an item in the ListView, I would like to be able to determine which item was clicked. I have the following code:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.listr);


   //setupDB();  
  // populateList3();

   ListView lv = (ListView) findViewById(R.id.ListView01);
   lv.setClickable(true);

   lv.setOnItemClickListener(new OnItemClickListener() {

       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           Toast.makeText(ListRecords.this,"Clicked!", Toast.LENGTH_LONG).show();
       }
   });

}

My XML code:


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

     android:id="@+id/Linear03lr"
     android:layout_width="fill_parent"  
     android:orientation="vertical"  
     android:gravity="center"
     android:layout_height="fill_parent">  


android:layout_width="wrap_content" android:layout_height="400dp" />

     <Button
        android:id="@+id/previousbutton"
        android:gravity="center_horizontal"
        android:layout_width = "fill_parent"
        android:layout_height = "fill_parent"

        android:text="Previous Menu"/>
</LinearLayout>  

What am I missing here to be able to intercept clicks on the Listview?

Thanks

© Stack Overflow or respective owner

Related posts about android

Related posts about listview