如何在吐司中显示arraylist的值?

时间:2016-12-05 22:06:36

标签: java android android-arrayadapter android-dialog

我在alertdialog中显示复选框。当用户单击OK时,toast应该像你选择了PHP,Java,JSON一样。现在,它显示ID。我怎样才能获得价值?

  Dialog dialog;
    final String[] items = {" Objective C", " JAVA", " JSON", " C#", "PHP"};
    final ArrayList itemsSelected = new ArrayList();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select Languages you know : ");
    builder.setMultiChoiceItems(items, null,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int selectedItemId,
                                    boolean isSelected) {
                    if (isSelected) {
                        itemsSelected.add(selectedItemId);
                    } else if (itemsSelected.contains(selectedItemId)) {
                        itemsSelected.remove(Integer.valueOf(selectedItemId));
                    }
                }
            })
            .setPositiveButton("Done!", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //Your logic when OK button is clicked
                    Toast.makeText(MainActivity.this,"You've selected "+itemsSelected,Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                }
            });
    dialog = builder.create();
    dialog.show();

[更新]我找到了解决方案,但似乎并不理想。现在它的工作是添加以下代码。

String one="";
                    String two="";
                    String three="";
                    String four="";

                    String zero="";
                    if (s.contains("0" ))
                    {
                        zero="Obj C ";
                    }
                    if (s.contains("1"))
                    {
                        one="JAVA ";
                    } if (s.contains("2"))
                    {
                        two="JSON ";
                    } if (s.contains("3"))
                    {
                        three="C# ";
                    } if (s.contains("4"))
                    {
                        four="PHP ";
                    }

1 个答案:

答案 0 :(得分:0)

这部分代码

new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int selectedItemId,
                                    boolean isSelected) {
                    if (isSelected) {
                        **itemsSelected.add(selectedItemId);**
                    } else if (itemsSelected.contains(selectedItemId)) {

itemsSelected.add(selectedItemId)只是将id添加到你的arraylist中。您不希望添加ID。由于您希望添加精确值,因此您可以简单地使用itemsSelected.add(items [selectedItemId])之类的内容,因为items数组包含您需要显示的值。当你在arraylist中放入正确的值时,你可以使用itemsSelected.toString()或其他东西显示你的吐司。您还需要处理所有其他检查。