ListView not responding to click events in Android

早过忘川 提交于 2019-12-05 07:44:54

add

android:descendantFocusability="blocksDescendants"

to the top ViewGroup of your items

upd:

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

Try like this way:

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();

        }

It seems like you're trying to get the Button to respond to onItemClick, which is used for the ListView.

EDIT:

In your getView method inside your implementation of ListAdapter (FriendsListAdapter) you should put this:

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

The Button is inside a ListView and won't respond to the container's Listener - also check out the Button's documentation: http://developer.android.com/reference/android/widget/Button.html for which Listeners you can use...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!