Dialog only first选项有效

时间:2013-04-15 02:09:51

标签: java android dialog

我正在尝试从listview onItemLongCLick激活一个SingleChoiceItems对话框,它有3个选项通过radiobuttons 1.查看个人资料2.发送消息3.删除朋友,从我设置的方式,只有第一个选项吐司适用于所有3个选项。 如何让选项2和3执行他们的操作而不是执行选项1的操作?

以下是我的对话框的代码

@Override
public Dialog onCreateDialog(int id) {
    String[] items = { "View Profile", "Send Message", "Remove Friend" };
    final DBAdapter db = new DBAdapter(this);
    db.open();

    switch (id) {
    case 0:
        return new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle("Select a option")
                .setSingleChoiceItems(items, id,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                int choice = 0;
                                // TODO Auto-generated method stub
                                mChoice = choice;
                            }
                        })

                .setPositiveButton("OK", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int choice) {
                        // TODO Auto-generated method stub
                        if (mChoice == 0) {
                            Toast.makeText(getBaseContext(), "Test 1",
                                    Toast.LENGTH_SHORT).show();
                        } else if (mChoice == 1) {
                            Toast.makeText(getBaseContext(), "Test2",
                                    Toast.LENGTH_SHORT).show();
                        } else if (mChoice == 2) {
                            TextView friends = (TextView) findViewById(R.id.textview_friends);
                            String deletedfriend = friends.getText()
                                    .toString();
                            db.DeleteFriends(deletedfriend);
                            Toast.makeText(getBaseContext(),
                                    "Friend Removed", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                }

                )

                .setNegativeButton("Cancel", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int choice) {

                    }
                })

                .create();

    }
    return null;
}

1 个答案:

答案 0 :(得分:1)

删除choice = 0;

.setSingleChoiceItems(items, id,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {

                            mChoice = which;
                        }
                    })