package mobile.android.ch07.multi.choice.dialog;

 import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ListView; public class Main extends Activity {
private String[] provinces = new String[] { "辽宁省", "山东省", "河北省", "福建省", "广东省", "全选" };
private ListView lv = null;
private boolean[] state = new boolean[] { false, true, false, true, false, false }; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void onClick_MultiChoiceDialog(View view) { final AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("选择省份")
.setMultiChoiceItems(provinces, state, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { if (whichButton == 5 && isChecked) {
// lv.setItemChecked(0, true);
// lv.setItemChecked(1, true);
// lv.setItemChecked(2, true);
// lv.setItemChecked(3, true);
// lv.setItemChecked(4, true);
SparseBooleanArray sb;
sb = lv.getCheckedItemPositions();
// int size=sb.size();
for (int i = 0; i <= 5; i++) {
if (sb.get(i) == false) {
lv.setItemChecked(i, true);
}
}
} else if (whichButton == 5 && !isChecked) {
// state = new boolean[6];
for (int i = 0; i < state.length; i++) {
state<i> = false;
}
lv.clearChoices();
// lv.invalidate();
// ad.onContentChanged();
// SparseBooleanArray sb;
// sb = lv.getCheckedItemPositions();
// // int size=sb.size();
// for (int i = 0; i <= 5; i++) {
// if (sb.get(i) == true) {
// lv.setItemChecked(i, false);
// }
// }
System.out.println("");
}
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int count = lv.getCount();
String s = "您选择了:";
for (int i = 0; i < provinces.length; i++) { if (lv.getCheckedItemPositions().get(i))
s += i + ":" + lv.getAdapter().getItem(i) + " "; }
if (lv.getCheckedItemPositions().size() > 0) {
new AlertDialog.Builder(Main.this).setMessage(s).show();
} else {
new AlertDialog.Builder(Main.this).setMessage("您未选择任何省份").show(); } }
}).setNegativeButton("取消", null).create();
lv = ad.getListView();
ad.show();
}
}

这个是网友给出的办法,我按照这个办法,结果还是不能实现反选,每次点击反选,显示出来的条目都没有反选,只有往上滚动列表,之前隐藏的条目才显示为反选。

后来在反选遍历列表的时候加入lv.setItemChecked(i, false)就好了,可以正常显示为反选。

后来调试之后发现前面那样虽然可以正常显示,但是实际得到的列表的ture、false值有出入,重新修改了代码:

 package mobile.android.ch07.multi.choice.dialog;

 import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ListView; public class Main extends Activity {
private String[] provinces = new String[] { "辽宁省", "山东省", "河北省", "福建省", "广东省", "全选" };
private ListView lv = null;
private boolean[] state = new boolean[] { false, true, false, true, false, false }; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void onClick_MultiChoiceDialog(View view) { final AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("选择省份")
.setMultiChoiceItems(provinces, state, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { if (whichButton == 5) {
for (int i = 0; i <= 5; i++) { lv.setItemChecked(i, isChecked);
state[i] = isChecked; }
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int count = lv.getCount();
String s = "您选择了:";
for (int i = 0; i < provinces.length; i++) { if (lv.getCheckedItemPositions().get(i))
s += i + ":" + lv.getAdapter().getItem(i) + " "; }
if (lv.getCheckedItemPositions().size() > 0) {
new AlertDialog.Builder(Main.this).setMessage(s).show();
} else {
new AlertDialog.Builder(Main.this).setMessage("您未选择任何省份").show(); } }
}).setNegativeButton("取消", null).create();
lv = ad.getListView();
ad.show();
}
}
05-11 18:27