1

I am building an alert box which has a char array of elements which is used as data for multiple selection checkbox. My question is how to make this alert box return the value as 1,2,3 depending on the selected item order? ie. if i select mercur and venus i have to get the value as 1,2. How will i implement this? As you can see after my try with the for loop it is just now printing which checkbox is checked. Please help me out!!!

  public class MultiActivity extends Activity {
  protected CharSequence[] _options = { "Mercury", "Venus", "Earth", "Mars",  
  "Jupiter", "Saturn", "Uranus", "Neptune" };
  protected boolean[] _selections =  new boolean[ _options.length ];

protected Button _optionsButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _optionsButton = ( Button ) findViewById( R.id.button1);
    _optionsButton.setOnClickListener( new ButtonClickHandler()  );
}


public class ButtonClickHandler implements View.OnClickListener {
    public void onClick( View view ) {
        showDialog( 0 );
    }
}

@Override
protected Dialog onCreateDialog( int id ) 
{
    return 
    new AlertDialog.Builder( this )
        .setTitle( "Planets" )
        .setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
        .setPositiveButton( "OK", new DialogButtonClickHandler() )
        .create();
}

public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
{
    public void onClick( DialogInterface dialog, int clicked, boolean selected )
    {
        Log.i( "ME", _options[ clicked ] + " selected: " + selected );
    }
}
public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
    public void onClick( DialogInterface dialog, int clicked )
    {
        switch( clicked )
        {
            case DialogInterface.BUTTON_POSITIVE:
                printSelectedPlanets();
                break;
        }
    }
}

protected void printSelectedPlanets(){
    for( int i = 0; i < _options.length; i++ ){
        Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
        String abc = _options[ i ] + " selected: " + _selections[i];
        TextView ab = (TextView)findViewById(R.id.textView1);
        ab.setText(abc);
        System.out.print(abc);

    }
}

}

See i got the below error printed in my logcat when i tried to run the below mentioned code.

11-21 14:23:59.905: E/AndroidRuntime(439): FATAL EXCEPTION: main 11-21 14:23:59.905: E/AndroidRuntime(439): java.lang.NullPointerException 11-21 14:23:59.905: E/AndroidRuntime(439): at com.workspace.multi.peek$2.onClick(peek.java:47) 11-21 14:23:59.905: E/AndroidRuntime(439): at com.android.internal.app.AlertController$AlertParams$4.onItemClick(AlertController.java:886) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.widget.ListView.performItemClick(ListView.java:3382) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.os.Handler.handleCallback(Handler.java:587) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.os.Handler.dispatchMessage(Handler.java:92) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.os.Looper.loop(Looper.java:123) 11-21 14:23:59.905: E/AndroidRuntime(439): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-21 14:23:59.905: E/AndroidRuntime(439): at java.lang.reflect.Method.invokeNative(Native Method) 11-21 14:23:59.905: E/AndroidRuntime(439): at java.lang.reflect.Method.invoke(Method.java:521) 11-21 14:23:59.905: E/AndroidRuntime(439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-21 14:23:59.905: E/AndroidRuntime(439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-21 14:23:59.905: E/AndroidRuntime(439): at dalvik.system.NativeStart.main(Native Method) 11-21 14:33:05.145: D/AndroidRuntime(495): Shutting down VM 11-21 14:33:05.145: W/dalvikvm(495): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 11-21 14:33:05.166: E/AndroidRuntime(495): FATAL EXCEPTION: main 11-21 14:33:05.166: E/AndroidRuntime(495): java.lang.NullPointerException 11-21 14:33:05.166: E/AndroidRuntime(495): at com.workspace.multi.MultiActivity$1.onClick(MultiActivity.java:51) 11-21 14:33:05.166: E/AndroidRuntime(495): at com.android.internal.app.AlertController$AlertParams$4.onItemClick(AlertController.java:886) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.widget.ListView.performItemClick(ListView.java:3382) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.os.Handler.handleCallback(Handler.java:587) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.os.Handler.dispatchMessage(Handler.java:92) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.os.Looper.loop(Looper.java:123) 11-21 14:33:05.166: E/AndroidRuntime(495): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-21 14:33:05.166: E/AndroidRuntime(495): at java.lang.reflect.Method.invokeNative(Native Method) 11-21 14:33:05.166: E/AndroidRuntime(495): at java.lang.reflect.Method.invoke(Method.java:521) 11-21 14:33:05.166: E/AndroidRuntime(495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-21 14:33:05.166: E/AndroidRuntime(495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-21 14:33:05.166: E/AndroidRuntime(495): at dalvik.system.NativeStart.main(Native Method)

Froyo
  • 455
  • 1
  • 8
  • 21

1 Answers1

3

Check this...

 ArrayList<Integer> pos=new ArrayList<Integer>();
final CharSequence str[]={"Android","Black Berry","Iphone"};
    private void MultipleChoiceSpinner() {
        // TODO Auto-generated method stub


        AlertDialog.Builder builder=new AlertDialog.Builder(TestGalleryActivity.this).setMultiChoiceItems(str, default_selected, new  DialogInterface.OnMultiChoiceClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) 
            {
            if(isChecked)
            {
                pos.add(which);
                default_selected[which]=true;
            }
                else{
                    pos.remove(pos.indexOf(which));
                    default_selected[which]=false;
                }


            }
        }).setTitle("Select Any");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(TestGalleryActivity.this, "Selected positions:"+pos.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        d=builder.create();
        d.show();

    }

Hi, I am just add two types of alert boxes here. I hope that it will be useful to someone.

Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
  • See i don't want to bulid it using an integer array. I want it by a string or char array coz i want both the values like I wanted which all items selected as String like Mercury, Venus and also as Integer by position 2,4. – Froyo Nov 21 '11 at 07:43
  • There is integer array with positions,so you have to get values with it. for(int p:pos){ String s=str[p].toString(); }//Here p is position,str is Charsequence array.... – Ramesh Akula Nov 21 '11 at 08:29