java - AlertDialog SingleChoiceItems 不起作用

标签 java android eclipse android-alertdialog

我正在尝试制作一个带有“确定”和“取消”按钮的 SingleChoiceItems AlertDialog,该按钮为用户提供了从 ListView 的 onItemLongClick 上的选项,以 1.查看用户的个人资料 2.向用户发送消息并 3.删除用户从好友列表中。

我还没有完成选项 1 和 2,但我现在正在尝试制作选项 3,其中包含数据库删除方法和一个显示“好友已删除”的 toast ,但是当我选择选项 3 并单击“确定”时该行不会被删除,或者我看到 Toast 说用户已被删除。这是我正在使用对话框的 Activity 的代码。

public class PlayAFriend extends ListActivity {
    DBAdapter DBAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_items);
        final DBAdapter db = new DBAdapter(this);
        DBAdapter = db.open();
        ListView FriendLV = (ListView) findViewById(android.R.id.list);
        final Cursor friendslist = db.GetAllFriends();
        String[] from = new String[] { "FRIENDS" }; // your column/columns here
        int[] to = new int[] { R.id.textview_friends };

        @SuppressWarnings("deprecation")
        ListAdapter cursorAdapter = new SimpleCursorAdapter(this,
                R.layout.list_items, friendslist, from, to, 0);
        FriendLV.setAdapter(cursorAdapter);
        FriendLV.setLongClickable(true);

        FriendLV.setOnItemLongClickListener(new OnItemLongClickListener() {

            class CustomCursorAdapter extends CursorAdapter {
                public CustomCursorAdapter(Context context, Cursor c, int flags) {
                    super(context, friendslist, flags);
                    // TODO Auto-generated constructor stub
                    inflater = LayoutInflater.from(context);
                }

                LayoutInflater inflater;

                @Override
                public void bindView(View view, Context context,
                        Cursor friendslist) {
                    // TODO Auto-generated method stub
                    int row_id = ((com.fullfrontalgames.numberfighter.DBAdapter) friendslist)
                            .get("_id");

                }

                @Override
                public View newView(Context context, Cursor friendslist,
                        ViewGroup parent) {
                    // TODO Auto-generated method stub
                    LayoutInflater inflater = LayoutInflater.from(context);
                    View v = inflater.inflate(R.layout.list_items, parent,
                            false);
                    bindView(v, context, friendslist);
                    return v;
                }
            }

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                showDialog(arg2);
                return false;

            }
        });

    }

    public void OnButtonClick(View view) {
        startActivity(new Intent(
                "com.fullfrontalgames.numberfighter.Fightattacker"));
    }

    String[] items = { "View Profile", "Send Message", "Remove Friend" };

    @Override
    public Dialog onCreateDialog(final int id) {
        final DBAdapter db = new DBAdapter(this);
        db.open();
        switch (id) {
        case 0:
            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("Select an Option")
            .setSingleChoiceItems(items, id,
                    new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog,
                        int choice) {
                    // TODO Auto-generated method stub

                }
            }

                    )
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {

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

                            // TODO Auto-generated method stub

                            if (choice == 0) {
                                Toast.makeText(getBaseContext(),
                                        "Test", Toast.LENGTH_SHORT)
                                        .show();
                            } else if (choice == 1) {
                                Toast.makeText(getBaseContext(),
                                        "Test2", Toast.LENGTH_SHORT)
                                        .show();

                            } else if (choice == 2) {
                                Toast.makeText(getBaseContext(),
                                        "Friend Removed",
                                        Toast.LENGTH_SHORT).show();
                                final TextView friends = (TextView) findViewById(R.id.textview_friends);
                                String deletedfriend = friends
                                        .getText().toString();

                                db.DeleteFriends(deletedfriend);

                            }

                        }

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

                        }
                    })

                    .create();

        }
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Clear the database
        DBAdapter.close();

    }

}

最佳答案

选择单选按钮时必须保存选择,按钮中的其他 int 参数不代表选择。 使 int mChoice; 成为类成员

public class PlayAFriend extends ListActivity {
DBAdapter DBAdapter;
int mChoice;
......
......

@Override
public Dialog onCreateDialog(final int id) {

    final DBAdapter db = new DBAdapter(this);
    db.open();
    switch (id) {
    case 0:
        return new AlertDialog.Builder(this)
        .setIcon(R.drawable.icon)
        .setTitle("Select an Option")
        .setSingleChoiceItems(items, id,
                new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog,
                    int choice) {
                mChoice = choice; // save the choice

            }
        }

                )
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {

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

                        // TODO Auto-generated method stub

                        if (mChoice == 0) {
                            Toast.makeText(getBaseContext(),
                                    "Test", Toast.LENGTH_SHORT)
                                    .show();
                        } else if (mChoice == 1) {
                            Toast.makeText(getBaseContext(),
                                    "Test2", Toast.LENGTH_SHORT)
                                    .show();

                        } else if (mChoice == 2) {
                            Toast.makeText(getBaseContext(),
                                    "Friend Removed",
                                    Toast.LENGTH_SHORT).show();
                            final TextView friends = (TextView) findViewById(R.id.textview_friends);
                            String deletedfriend = friends
                                    .getText().toString();

                            db.DeleteFriends(deletedfriend);

                        }

                    }

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

                    }
                })

                .create();

    }
    return null;
}

关于java - AlertDialog SingleChoiceItems 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15995927/

相关文章:

Android - Firebase ServerValue.TIMESTAMP 返回 "{.sv=timestamp}"

java - 创建的pom.xml在哪里

java - onClick 到另一个 JSP 不起作用

java - 更改 JLabel 的一部分的字体?

java - 无法让 JBehave 元过滤器从 mvn 命令行工作

android - 接收外语 Json(例如俄语)Android

android - 如何将 utf8 字符数据从 android 插入到 codeigniter 服务器

java - 添加天数后如何在 Joda-Time 中获得准确的月份差异

java - 将 JScrollPane 添加到 JPanel

java - Windows 上的 Eclipse : Exception in thread "main" java. lang.UnsupportedClassVersionError : MainMethod : Unsupported major. 次要版本 52.0