setMultiChoiceItems和我的数组

时间:2016-10-14 10:43:40

标签: java android arrays alertdialog android-alertdialog

在我的代码中,我可以选择复选框项目并在我的数组中设置它们:

** 0.5

但是当我选择相同的复选框项时,需要从我的数组中删除它...并且我不能这样做,因为我的数组中的id与我的sqrt不同。 如何删除我想要的项目?

点击肯定按钮后,我可以获得所有选定的项目吗?

use strict;
use warnings;

sub dist {
    my ($p1, $p2) = @_;
    return sqrt(($p1->{x} - $p2->{x}) ** 2 + ($p1->{y} - $p2->{y}) ** 2);
}

my %dot1 = (x => 5, y => 6);
my %dot2 = (x => 7, y => 8);

my $D = dist(\%dot1, \%dot2);

print "Result: $D\n";

2 个答案:

答案 0 :(得分:1)

您可以通过以下方法删除项目。

    ArrayList<Integer> selectedStatusId = new ArrayList<>();
    if (true){
        selectedStatusId.add(mStatuses.get(i).StatusId);
    } else {
        //  delete the first occurrence of the specified element from array
        selectedStatusId.remove(new Integer(mStatuses.get(i).StatusId));
    }

答案 1 :(得分:0)

 final ArrayList<String> selectedStatusId = new ArrayList<>();
        //MAKE IT STRING TYPE

        final ArrayList<String> statusesTitles = new ArrayList<>();
        for (int i = 0; i < mStatuses.size(); i++) {
            statusesTitles.add(mStatuses.get(i).StatusTitle);
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.order_dialog_status_title)
                .setMultiChoiceItems(statusesTitles.toArray(new String[statusesTitles.size()]), null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        if (b) {
                            selectedStatusId.add(String.valueOf(mStatuses.get(i).StatusId));
                        } else {
                            // TODO How I can delete my position from array?
                            selectedStatusId.remove(String.valueOf(mStatuses.get(i).StatusId));
                        }
                        //OR_______________________YOU CAN USE THIS ALSO
//                        if (selectedStatusId.contains(String.valueOf(mStatuses.get(i).StatusId))) {
//                            selectedStatusId.remove(String.valueOf(mStatuses.get(i).StatusId));
//                        } else {
//                            selectedStatusId.add(String.valueOf(mStatuses.get(i).StatusId));
//                        }
                    }
                })
                .setPositiveButton(R.string.order_dialog_status_positive_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        refreshContent();
                    }
                })
                .setNegativeButton(R.string.order_dialog_status_negative_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });
        builder.show();

希望这会有所帮助..