Listview单击侦听器没有响应

时间:2014-06-22 07:32:34

标签: android listview

我有一个列表视图,想要在其上执行可点击的事件。我之前做过。但现在它不起作用。我也添加了我的XML。我只需要在列表视图中点击一个动作即可。

      CustomFinalSubmit_ItemDetail item = new  CustomFinalSubmit_ItemDetail(Final_Submit.this , R.layout.customview_finalsubmit_itemdetails, itemInfo);
    itemList.setAdapter(item);
    itemList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            FinalSubmitItem pos = itemInfo.get(position);
            String itemType = pos.getItemType();
            String itemCountry = pos.getItemCountry();
            String itemSerial = pos.getItemNo();
            pos.setChecked(true);

            Intent inn = new Intent(getApplicationContext(), FinalSubmitDetails.class);
            inn.putExtra("itemType", itemType);
            inn.putExtra("itemCountry", itemCountry);
            inn.putExtra("itemSerial", itemSerial);
            startActivity(inn);
        }

    });

这是我的主要Xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/green">  
<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_marginTop="10dip"
android:orientation="vertical"
android:background="#0B3B0B">
<ListView 
 android:id="@+id/CustomerDetailList"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/yellow"/>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:orientation="vertical">
   <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:id="@+id/Itemtype"
        android:paddingTop="5dp"
        android:background="@color/green"
        android:textColor="@color/yellow"
        android:layout_marginLeft="5dip"
        android:text="Item Type"/>
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:paddingTop="5dp"
        android:id="@+id/txtcountry"
        android:background="@color/green"
        android:textColor="@color/yellow"
        android:text="Country"/>
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:paddingTop="5dp"
        android:id="@+id/txtItemNumber"
        android:background="@color/green"
        android:textColor="@color/yellow"
        android:text="Item No."/>
     <CheckBox 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:id="@+id/itemChck"
        android:button="@drawable/custom_checkbox"
        android:paddingTop="5dp"
        android:padding="5dp"/>  
        </LinearLayout>  
 <View
  android:layout_width="match_parent"
  android:layout_margin="5dp"
  android:layout_height="1dp"
  android:background="@color/white"/> 
<ListView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/itemList"
    android:background="#088A08"
    android:divider="@color/white"
    android:dividerHeight="1dp"/>
</LinearLayout>

</LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

我是这样做的:

            convertView.setOnClickListener(new View.OnClickListener() {                 
                @Override
                public void onClick(View v) {
                                     your code
                }});

只需将这么多代码添加到我的自定义视图

即可

答案 1 :(得分:0)

最近,在将ToggleButton添加到列表项后,我遇到了同样的问题。我的解决方案是将android:descendantFocusability="blocksDescendants"添加到项目的布局中。

以下是整个XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:paddingLeft="15dp"
    android:descendantFocusability="blocksDescendants" >

    <TextView
        android:id="@+id/watch_roadName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Addresse"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <ToggleButton
        android:id="@+id/watch_button_arrived"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/watch_roadName"
        android:layout_alignParentRight="true"
        android:textOn="@string/arrived"
        android:textOff="@string/arrived"
        android:text="@string/arrived" />

</RelativeLayout>

同样问题的类似答案可以在这里找到:

How to fire onListItemClick in Listactivity with buttons in list?

在这里

Android custom ListView with ImageButton is not getting focus

答案 2 :(得分:0)

在您的充气机布局的小部件(Textview或按钮)中添加:

android:focusable="false"
android:focusableInTouchMode="false"

并在充气机的父布局中添加:

android:descendantFocusability="blocksDescendants"