开发者

ListView in Android, handler for clicking item

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 layout.xml:

<?xml version="1.0" encoding开发者_开发问答="utf-8"?>  
<LinearLayout  
    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">  


 <!--Put form controls here-->  
    <ListView android:id="@+id/ListView01"

 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?


It might be easier to use ListActivity, then you simply have to over-ride onListItemClick.


Assuming you are populating the listview (right now there is nothing in it), the clicked item's position is given in the 'position' argument of your onitemclick method.

While developing, I suggest logging information with log.v("text1","text2") and check the DDMS console to see what your application is actually executing.


It looks like you have the clickListener set up correctly. With this code are you getting the Toast popup when you click on an item in the list? The int position variable that gets passed to the onClick() method will let you see which item in the list has been clicked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜