我正在构建一个警告框,其中包含一个char数组元素,用作多选复选框的数据。我的问题是如何使此警报框返回值为1,2,3,具体取决于所选的项目顺序?即。如果我选择mercur和venus,我必须得到1,2的值。我将如何实现这一点?正如我在尝试使用for循环后所看到的那样,现在正在打印哪个复选框被选中。请帮帮我!!!
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);
}
}
}
当我尝试运行下面提到的代码时,看到我的logcat中打印了以下错误。
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)
答案 0 :(得分:3)
检查一下......
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();
}
嗨,我只是添加了两种警告框here。我希望它对某人有用。