6

我有一个自定义ListView项目如下:

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

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

现在我希望在任何列表项上单击Ping Friend按钮时,它应该显示一个AlertDialog。我用过这段代码...

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

没有显示错误。

问题是它没有响应 Click on Button 并且根本没有出现 AlertDialog。

对于按钮,我也厌倦了在onItemClickListView 中添加

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

}

还是没有反应。

请建议。

4

3 回答 3

20

添加

android:descendantFocusability="blocksDescendants"

到项目的顶部 ViewGroup

更新

<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>
于 2013-03-27T07:25:44.840 回答
0

似乎您正试图让Button响应onItemClick,它用于ListView.

编辑:

在(FriendsListAdapter)getView实现中的方法中,ListAdapter您应该输入以下内容:

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

位于Buttona 内ListView并且不会响应容器的Listener- 另请查看Button's文档:http: //developer.android.com/reference/android/widget/Button.htmlListeners可以使用...

于 2013-03-27T07:26:12.767 回答
0

像这样尝试:

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

        }
于 2013-03-27T07:10:34.907 回答