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)