Search code examples
androidlistviewonclicklistenerandroid-adapterview

AdapterView.OnItemClickListener() not fired


My Adapter is set like this:

userList = (ListView) activity.findViewById(R.id.userList);
userListAdapter = new UserListAdapter(this, R.layout.user_list, getUserList());
userList.setAdapter(userListAdapter);

function getUserList() return ArrayList UserList

Like that the list is showed no problem on that.

But when I do this:

userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("MyTag","kkkkkkkkkkkkkkkkk");
            }
        });

the Log.d is not fired. I looked up in the console is shows D/AbsListView: onTouchUp() mTouchMode : 1

My Xmls activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/userList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="MainActivity"
    tools:showIn="@layout/activity_main">
</ListView>

user_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:weightSum="60"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/nom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="20"/>

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="20"/>

    <Button
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:hint="Upd"/>

    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:hint="Del"/>
</LinearLayout>

Solution

  • In your adapter make one interface for handle click event like below code..

    onItemClickListner onItemClickListner;
    
    public void setOnItemClickListner(UserAdapter.onItemClickListner onItemClickListner) {
        this.onItemClickListner = onItemClickListner;
    }
    
    public interface onItemClickListner{
       public void onItemClick();
    }
    

    when any control used to click event like hole layout click event used below code in adapter ..In adapter getView() method

     LinearLayout linearLayout=view.findViewById(R.id.linearLayout);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListner.onItemClick();
            }
        });
    

    after when you bind adapter in list view then after called below ...

            userListAdapter.setOnItemClickListner(new UserAdapter.onItemClickListner() {
            @Override
            public void onItemClick() {
                Toast.makeText(getApplicationContext(),"Hello world",Toast.LENGTH_SHORT).show();
            }
        });