0

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

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

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

    }

}
4

1 回答 1

1

选择单选按钮时必须保存选择,按钮中的其他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;
}
于 2013-04-14T05:26:46.433 回答