ListView setOnItemClickListener not working in custom list view
Set these properties
android:focusable="false"
android:focusableInTouchMode="false"
for your all UI elements in your create_list_item xml file.
Also remove that properties from ListView.
So your ListView will be
<ListView
android:id="@+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
Custom ListView setOnItemClickListener
If you use clickable buttons inside , I think, the best way is not to use AdapterView.OnItemClickListener
but the usual onClick
event.
Or use android:descendantFocusability="blocksDescendants"
I you wish your case.
How to make a setOnItemClickListener work for a ListView with a CustomAdapter?
Problem solved
I just added this code inside the custom_list_view.xml and finally setOnClickListener worked.
android:descendantFocusability="blocksDescendants"
Now my layout is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">
setOnclicklistener in a listview with a customadapter
On your activity you should add a listener to the listview.
list.setOnItemClickListener { parent, view, position, id ->
val myItem = parent.getItemAtPosition(position) as MyDataObj
}
You should also have the getItem() method filled correctly on your adapter as you do.
Android custom ListView item setOnItemClickListener not working
Can you add this code?
android:descendantFocusability="blocksDescendants"
payment_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:paddingLeft="10dp">
<ImageView
android:layout_width="46dp"
android:layout_height="35.3dp"
android:textColor="#000000"
android:id="@+id/payment_item_name2"
android:layout_gravity="center_horizontal|start"
android:src="@drawable/payments_internet"
android:textSize="20sp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8c8c8c"
android:id="@+id/payment_item_name"
android:layout_gravity="center_horizontal|center"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:layout_marginTop="12dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:src="@drawable/arrow_right_red"
android:layout_gravity="center_vertical|right"
android:id="@+id/backButton"
android:textAllCaps = "true"
/>
</LinearLayout>
ListView setOnItemClickListener not work
In your ListView
remove the xml attribute android:clickable="true"
This is taking your click event and not sending it down to the child views.
Edit
You are passing in getApplicationContext()
which is the wrong context. The listener does work, but the Toast is not displaying in the right context. You need to pass in getContext()
. However, since this is an Activity, you can just pass in this
instead.
public void outputListView(ArrayList<String> list) {
Adapter adapter = new Adapter(this, R.layout.item, list);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
Related Topics
Device Not Detected in Eclipse When Connected with Usb Cable
Activity Layout: Fragment Class: VS Android:Name Attributes
Linkedlist Put into Intent Extra Gets Recast to Arraylist When Retrieving in Next Activity
Force Update of an Android App When a New Version Is Available
How to Determine If One of My Activities Is in the Foreground
No Activity Found to Handle Intent:Android.Intent.Action.View
How to Store Images Using Sharedpreference in Android
Phonegap - Forcing Landscape Orientation
Android: How to Get a Radiogroup with Togglebuttons
How to Open a PDF via Intent from Sd Card
Proguard Causing Runtimeexception (Unmarshalling Unknown Type Code) in Parcelable Class
How to Set Custom Actionbar Color/Style
How to Block a Mobile Number Call and Message Receiving in Android Application Development
Androidx:Appcompat I:Art Error Android.View.View$Onunhandledkeyeventlistener