Android - Listview项setonitemclicklistener不工作

时间:2014-12-22 10:33:25

标签: java android listview onitemclicklistener

下面是我的listview代码,点击它时遇到问题,当itemclick时它甚至不显示toast,这意味着当我点击时onitemclicklistener没有收听

    itemList = new ArrayList<PostList>();
    applianceList = (ListView)findViewById(R.id.listView1);
    Button add = new Button(this);
    add.setBackgroundResource(R.drawable.add_button);
    applianceList.addFooterView(add);

    itemAdapter = new PostAdapterList(MainActivity.this, itemList);
    applianceList.setAdapter(itemAdapter);  

    applianceList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "here", Toast.LENGTH_LONG).show();
        }
    });

主要活动的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:id="@+id/main_activity"
 >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.thesis.Appliance"
        android:layout_width="225dp"
        android:layout_height="225dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView01"
        android:layout_marginRight="25dp"
        android:layout_marginTop="15dp" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/fragment1"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:background="#3D76AD" >
    </ListView>

    <TextView
        android:id="@+id/fill"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listView1"
        android:layout_below="@+id/TextView01"
        android:background="#3D76AD" />

    <TextView
        android:id="@+id/fill2"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/fill"
        android:layout_alignParentTop="true"
        android:background="#3D76AD" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fill2"
        android:background="#3D76AD"
        android:paddingLeft="10dp"
        android:text="Profile"
        android:textColor="#000000"
        android:textSize="30dp" />

</RelativeLayout>

</LinearLayout>

listview适配器的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:paddingLeft="5dp">

<ImageView
    android:id="@+id/appliance_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/defaultimage" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

设置

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

表示您的自定义布局文件中ImageView在列表适配器中膨胀。

相关问题