简体   繁体   中英

Android - Listview item setonitemclicklistener not working

Below is my code for listview, got a problem clicking it, it wont even show the toast when itemclick, that means the onitemclicklistener was not listening when i click

    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 file for mainactivity

<?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>

xml file for the listview adapter

<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>

Set

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

for your ImageView in your custom layout file which is inflated in your list adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM