1

我正在尝试获取数组中所选项目的值。这是我的代码:

    final String items[] = {"Blue","Green","Orange"};
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);  


    alert.setTitle("Money Maker");
    alert.setIcon(R.drawable.ic_launcher);


    alert.setMultiChoiceItems(items, checkedItems,  new 
    DialogInterface.OnClickListener() {

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

            String value = items.toString();
            Toast.makeText(getApplication(), value, Toast.LENGTH_LONG).show();
        }
    });


    alert.show();

如何获取所选项目的值并将其显示在 Toast 中?先感谢您。

4

6 回答 6

3

试试这个进行多项选择

  //store all selected items in arraylist
 ArrayList<String> selectedItems=new ArrayList<String>();
 final CharSequence[] items = { "Gujrat", "Rajasthan",
                    "Maharastra", "Panjab", "Madhya Pradesh", "Hariyana",
                    "Bihar" };
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Select State");

            builder.setMultiChoiceItems(items, null,
                    new DialogInterface.OnMultiChoiceClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which, boolean isChecked) {
                            // TODO Auto-generated method stub
                            if (isChecked){
                                Toast.makeText(getApplicationContext(),
                                        items[which], Toast.LENGTH_SHORT)
                                        .show();
                              selectedItems.add(items[which]);
                           }else{
                                   if(selectedItems.contains(items[which])
                                          selectedItems.remove(items[which]);
                           }
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
于 2013-08-14T05:23:31.347 回答
2

从 MultiChoice AlertDialog 中获取选定项目。

ArrayList<String> selectedItems=new ArrayList<String>();
        final CharSequence[] items = { "Blue","Green","Orange"};
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        builder.setTitle("Money Maker");

        builder.setMultiChoiceItems(items, null,new OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked){
                    selectedItems.add(items[which].toString());
                }
                else{
                   if(selectedItems.contains(items[which].toString()))
                       selectedItems.remove(items[which].toString());
                }
            }
        });
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Get all selectedItems here..
                Toast.makeText(mActivity, "Total selected item ="+selectedItems.size(), Toast.LENGTH_SHORT).show();
                for (int j = 0; j < selectedItems.size(); j++) {
                    System.out.println("Selected item.."+selectedItems.get(j));
                }
            }
        });

       AlertDialog alert = builder.create();
       alert.show();

mActivity是您的活动参考。

于 2013-08-14T05:41:56.800 回答
1

a) 显示选中的值以及删除选中的值和删除取消选中的值

Vector selectedItems = new Vector();
String items[] = { "Blue", "Green", "Orange" };
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Money Maker");
alert.setIcon(R.drawable.ic_launcher);

builder.setMultiChoiceItems(items, null,
    new DialogInterface.OnMultiChoiceClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which, boolean isChecked) {
          if (isChecked) {
              Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
              selectedItems.addElement(items[which]);
          } else {
                if (selectedItems.contains(items[which])) {
                    selectedItems.removeElement(items[which]);                      
           }
}
}).show();

b)仅显示选定的值您可以直接传递我们在which变量中获得的位置

Toast.makeText(getApplication(), items[which], Toast.LENGTH_LONG).show();
于 2013-08-14T05:19:39.143 回答
1

尝试这个

alert.setMultiChoiceItems(items, null,
                new OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {
                        Toast.makeText(getApplication(), items[which],
                                Toast.LENGTH_LONG).show();

                    }
                });
于 2013-08-14T05:28:09.847 回答
0

只是参考items[wich]- 那是你的项目

于 2013-08-14T05:18:49.103 回答
0

您需要指定所选项目的索引:

String value=items[which].toString();

其余代码保持不变。

于 2013-08-14T05:22:51.073 回答