我有一个对话框checkbox,我试图做不同的事情时,select选项,当好的按下。 我以为在阅读完一些教程之后,我知道自己在做什么,但是当我按下好的时候,即使没有被选中,也只是为了“一切”。 所以看来我的if语句不能正常工作,但我不知道为什么。
任何build议,我做错了什么,以及如何解决它将不胜感激!
final CharSequence[] items = {"Item 1", "Item 2", "Item 3"}; final boolean[] states = {false, false, false}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("What would you like to do?"); builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener(){ public void onClick(DialogInterface dialogInterface, int item, boolean state) { } }); builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { SparseBooleanArray CheCked = ((AlertDialog)dialog).getListView().getCheckedItemPositions(); if(CheCked.get(CheCked.keyAt(0)) == true){ Toast.makeText(Backup.this, "Item 1", Toast.LENGTH_SHORT).show(); } if(CheCked.get(CheCked.keyAt(1)) == true){ Toast.makeText(Backup.this, "Item 2", Toast.LENGTH_SHORT).show(); } if(CheCked.get(CheCked.keyAt(2)) == true){ Toast.makeText(Backup.this, "Item 3", Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.create().show(); }
问题是你使用keyAt
,根据API Docs
从这个SparseBooleanArray存储的indexth键值映射中返回键值。
而这个SparseBooleanArray
只包含你的列表( SparseBooleanArray
的检查项目!
所以如果你检查两个三项,那么CheCked
成员将包含2个项目,当你调用keyAt(2)
,它将返回0(不是IndexOutOfBoundsException
,尽pipe在该位置地图不具有值)。
你应该使用的只是这个SparseBooleanArray
的get
方法,以ListView
的项目位置作为参数:
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { SparseBooleanArray CheCked = ((AlertDialog) dialog).getListView().getCheckedItemPositions(); if (CheCked.get(0)) { Toast.makeText(TmpActivity2.this, "Item 1", Toast.LENGTH_SHORT).show(); } if (CheCked.get(1)) { Toast.makeText(TmpActivity2.this, "Item 2", Toast.LENGTH_SHORT).show(); } if (CheCked.get(2)) { Toast.makeText(TmpActivity2.this, "Item 3", Toast.LENGTH_SHORT).show(); } } });
你也可以放弃if子句的== true
部分,但这只是语义。
如果查看getCheckedItemPositions
方法的API Docs ,可以find这个解决scheme:
返回 :
一个SparseBooleanArray
,它将为每个get(int position)
调用返回true,其中position
是列表中的某个位置;如果select模式设置为CHOICE_MODE_NONE
则返回CHOICE_MODE_NONE
。
我会这样做,例如:
public class Main extends Activity { CharSequence[] items = {"Google", "Apple", "Microsoft"}; boolean[] itemsChecked = new boolean[items.length]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btnDialog); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: return new AlertDialog.Builder(this) .setIcon(R.drawable.icon) .setTitle("Dialog with simple text") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { for (int i = 0; i < items.length; i++) { if (itemsChecked[i]) { Toast.makeText(getBaseContext(), items[i] + " checked!", Toast.LENGTH_LONG).show(); } } } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_LONG).show(); } }) .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), items[which] + (isChecked ? "checked!" : "unchecked!"), Toast.LENGTH_SHORT).show(); } }) .create(); } return null; } }
这应该有所帮助
.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText( getBaseContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show(); } })
但是,你如何使用另一课的托运物品呢?