如何实现onItemClickListener在活动列表 [英] How to implement onItemClickListener in List Activity

查看:143
本文介绍了如何实现onItemClickListener在活动列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的ListActivity,的onCreate code

following is my ListActivity, onCreate code

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            fakeTripBuilder();

            setContentView(R.layout.cutom_list_view);

            SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.custom_row_view, 
                    new String[] {"stop","distance","time","icon","mode"},
                    new int[] {R.id.text_stop,R.id.text_distance, R.id.text_time, R.id.icon, R.id.text_mode});

            populateList();
            setListAdapter(adapter);
}

里面的同班我有以下方法

inside the the same class i have the following method

 @Override
       protected void onListItemClick(ListView l, View v, int position, long id){
           super.onListItemClick(l, v, position, id);
           Log.d("Travel","You choosed!");
           Object o = this.getListAdapter().getItem(position);
           Toast.makeText(this, "You Choosed"+o.toString(), Toast.LENGTH_LONG).show();
       }

和这里是custom_list_view.xml

and here is the "custom_list_view.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="wrap_content"
    android:clickable="false"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >
<ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000fff"
    android:layout_weight="2"
    android:drawSelectorOnTop="false">
</ListView>
<TextView 
    android:id="@id/android:empty"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFff00"
    android:text="No data"
/>
</LinearLayout>

问题是,当我点击它onListItemClick方法不调用。帮助!

Problem is, onListItemClick method do not call when i click on it. Help !!

推荐答案

要在ListActivity,你应该调用get到ListView的引用:

To get a reference to the ListView in a ListActivity you should call:

ListView lv = getListView();

您不需要设置一个点击监听器,它已经有一个。简单地覆盖:

You do not need to set a click listener, it already has one. Simply override:

 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
     Object item = lv.getAdapter().getItem(position);

 }

和在XML文件中,在ListView应该有ID:

And in your xml file, the ListView should have the id:

android:id="@android:id/list" 

修改

您需要删除

android:clickable="false"

从布局,以允许被点击任何子次

from the layout to allow any child views to be clicked

这篇关于如何实现onItemClickListener在活动列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆