ListView no responde a los eventos de clics en Android

Tengo un artículo ListView personalizado como sigue:

 <?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="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:paddingTop="2dp" android:paddingBottom="2dp" android:background="@android:color/transparent"> <ImageView android:id="@+id/profile_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/mr_unknown" android:contentDescription="@string/profile_picture_description" android:paddingRight="3dp"/> <TextView android:id="@+id/real_life_name" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/FriendListText"/> <Button android:id="@+id/ping_friend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ping Friend" /> </LinearLayout> 

Que se utiliza en este ListView:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@color/white" tools:context=".FriendListActivity" > <ListView android:id="@+id/friend_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:fastScrollEnabled="true" android:smoothScrollbar="false" style="@style/ListViewStyle" > </ListView> </LinearLayout> 

Ahora quiero que cuando el botón Ping Friend se hace clic en cualquier elemento de la lista debe mostrar un AlertDialog . He utilizado este código …

 friendListAdapter = new FriendListAdapter(FriendListActivity.this, friends); friendListView = (ListView) findViewById(R.id.friend_list); friendListView.setAdapter(friendListAdapter); downloadFriends_async(); //This method downloads all the *friends* into ListView from Database. *Its working correctly*. friendListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); alertDialogBuilder.setTitle("Hello!!!"); alertDialogBuilder .setMessage("Do you want to exit?") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { FriendListActivity.this.finish();}}) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel();}}); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } }); 

No se muestra ningún error.

El problema es que no responde a Click on Button y no AlertDialog aparece en absoluto.

Para el botón i también cansé de agregar dentro de onItemClick de ListView :

 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Button btnPingFriend = (Button) findViewById(R.id.ping_friend); btnPingFriend.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //Rest of AlertDialog Code... } 

Todavía no hay respuesta.

Por favor recomiende.

añadir

 android:descendantFocusability="blocksDescendants" 

A la parte superior ViewGroup de sus elementos

Actualizar

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:paddingTop="2dp" android:paddingBottom="2dp" android:background="@android:color/transparent" android:descendantFocusability="blocksDescendants" > <ImageView android:id="@+id/profile_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/mr_unknown" android:contentDescription="@string/profile_picture_description" android:paddingRight="3dp"/> <TextView android:id="@+id/real_life_name" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/FriendListText"/> <Button android:id="@+id/ping_friend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ping Friend" /> </LinearLayout> 

Prueba de esta manera:

 listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub AlertDialog.Builder alertbox=new AlertDialog.Builder(FirstActivity.this); alertbox.setTitle("Warning"); alertbox.setMessage("Exit Application?"); alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { /////operations } }); alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { } }); alertbox.show(); } 

Parece que usted está tratando de obtener el Button para responder a onItemClick , que se utiliza para el ListView .

EDITAR:

En su método getView dentro de su implementación de ListAdapter (FriendsListAdapter) debe poner esto:

 Button btnPingFriend = (Button) v.findViewById(R.id.ping_friend); btnPingFriend.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // Add your dialog here. } 

El Button está dentro de un ListView y no responderá al Listener del contenedor. También compruebe la documentación Button's : http://developer.android.com/reference/android/widget/Button.html para que Listeners puede utilizar .. .

  • Android - Gridview con Custom BaseAdapter, crear onclicklistener
  • excepción java.lang.NoClassDefFoundError en algunos dispositivos
  • Desactivación de onClickListener para Google Maps
  • Android: Detectar el botón ACTION_UP fuera del evento
  • Cómo desmarcar un elemento en gridview en segundo clic en android?
  • Cómo quitar un elemento seleccionado de ListView utilizando CursorAdapter
  • Acceder a objetos personalizados en una vista de lista
  • RecyclerView onItemClicked devolución de llamada
  • ¿Cómo dejar pasar el evento a container en android?
  • Usando un bucle para establecer los botones enclicklistener
  • ¿Por qué está cambiando donde se establecen los parámetros de diseño solucionando mi problema "OnClickListener no funciona para el primer elemento en GridView"?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.