1

I am trying to get the value for the selected item within the array. This is my code:

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

How do I get the value of the selected item and display it within a Toast? Thank you in advance.

user2673161
  • 1,675
  • 4
  • 23
  • 28

6 Answers6

3

try this for multiple selection

  //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();
        }
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
2

Get Selected items from 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();

Here mActivity is your activity's reference.

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1

a) To show the selected values as well as sore selected values and remove deselected values

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) To show only selected value You can directly pass the location which we get in which variable

Toast.makeText(getApplication(), items[which], Toast.LENGTH_LONG).show();
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
1

Try this

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

                    }
                });
Asha Soman
  • 1,846
  • 1
  • 18
  • 28
0

Just refer to items[wich] - that is your item

Maxim Efimov
  • 2,747
  • 1
  • 19
  • 25
0

You need to specify the index of the selected item:

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

Rest of the code remains the same.

Zax
  • 2,870
  • 7
  • 52
  • 76