1 package mobile.android.ch07.multi.choice.dialog; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.util.SparseBooleanArray; 8 import android.view.View; 9 import android.widget.ListView; 10 11 public class Main extends Activity { 12 private String[] provinces = new String[] { "辽宁省", "山东省", "河北省", "福建省", "广东省", "全选" }; 13 private ListView lv = null; 14 private boolean[] state = new boolean[] { false, true, false, true, false, false }; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 } 21 22 public void onClick_MultiChoiceDialog(View view) { 23 24 final AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("选择省份") 25 .setMultiChoiceItems(provinces, state, new DialogInterface.OnMultiChoiceClickListener() { 26 public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { 27 28 if (whichButton == 5 && isChecked) { 29 // lv.setItemChecked(0, true); 30 // lv.setItemChecked(1, true); 31 // lv.setItemChecked(2, true); 32 // lv.setItemChecked(3, true); 33 // lv.setItemChecked(4, true); 34 SparseBooleanArray sb; 35 sb = lv.getCheckedItemPositions(); 36 // int size=sb.size(); 37 for (int i = 0; i <= 5; i++) { 38 if (sb.get(i) == false) { 39 lv.setItemChecked(i, true); 40 } 41 } 42 } else if (whichButton == 5 && !isChecked) { 43 // state = new boolean[6]; 44 for (int i = 0; i < state.length; i++) { 45 state<i> = false; 46 } 47 lv.clearChoices(); 48 // lv.invalidate(); 49 // ad.onContentChanged(); 50 // SparseBooleanArray sb; 51 // sb = lv.getCheckedItemPositions(); 52 // // int size=sb.size(); 53 // for (int i = 0; i <= 5; i++) { 54 // if (sb.get(i) == true) { 55 // lv.setItemChecked(i, false); 56 // } 57 // } 58 System.out.println(""); 59 } 60 } 61 }).setPositiveButton("确定", new DialogInterface.OnClickListener() { 62 public void onClick(DialogInterface dialog, int whichButton) { 63 int count = lv.getCount(); 64 String s = "您选择了:"; 65 for (int i = 0; i < provinces.length; i++) { 66 67 if (lv.getCheckedItemPositions().get(i)) 68 s += i + ":" + lv.getAdapter().getItem(i) + " "; 69 70 } 71 if (lv.getCheckedItemPositions().size() > 0) { 72 new AlertDialog.Builder(Main.this).setMessage(s).show(); 73 } else { 74 new AlertDialog.Builder(Main.this).setMessage("您未选择任何省份").show(); 75 76 } 77 78 } 79 }).setNegativeButton("取消", null).create(); 80 lv = ad.getListView(); 81 ad.show(); 82 } 83 }
这个是网友给出的办法,我按照这个办法,结果还是不能实现反选,每次点击反选,显示出来的条目都没有反选,只有往上滚动列表,之前隐藏的条目才显示为反选。
后来在反选遍历列表的时候加入lv.setItemChecked(i, false)就好了,可以正常显示为反选。
后来调试之后发现前面那样虽然可以正常显示,但是实际得到的列表的ture、false值有出入,重新修改了代码:
1 package mobile.android.ch07.multi.choice.dialog; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.util.SparseBooleanArray; 8 import android.view.View; 9 import android.widget.ListView; 10 11 public class Main extends Activity { 12 private String[] provinces = new String[] { "辽宁省", "山东省", "河北省", "福建省", "广东省", "全选" }; 13 private ListView lv = null; 14 private boolean[] state = new boolean[] { false, true, false, true, false, false }; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 } 21 22 public void onClick_MultiChoiceDialog(View view) { 23 24 final AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("选择省份") 25 .setMultiChoiceItems(provinces, state, new DialogInterface.OnMultiChoiceClickListener() { 26 public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { 27 28 if (whichButton == 5) { 29 for (int i = 0; i <= 5; i++) { 30 31 lv.setItemChecked(i, isChecked); 32 state[i] = isChecked; 33 34 } 35 } 36 }).setPositiveButton("确定", new DialogInterface.OnClickListener() { 37 public void onClick(DialogInterface dialog, int whichButton) { 38 int count = lv.getCount(); 39 String s = "您选择了:"; 40 for (int i = 0; i < provinces.length; i++) { 41 42 if (lv.getCheckedItemPositions().get(i)) 43 s += i + ":" + lv.getAdapter().getItem(i) + " "; 44 45 } 46 if (lv.getCheckedItemPositions().size() > 0) { 47 new AlertDialog.Builder(Main.this).setMessage(s).show(); 48 } else { 49 new AlertDialog.Builder(Main.this).setMessage("您未选择任何省份").show(); 50 51 } 52 53 } 54 }).setNegativeButton("取消", null).create(); 55 lv = ad.getListView(); 56 ad.show(); 57 } 58 }