0
我有alertdialog自定義複選框,我需要將選定的選項傳遞給宿主活動,並堅持選定的選項。這裏是我的代碼:動態的方式來使用複選框
public class TimelineSettings extends DialogFragment {
ArrayList<Integer> selected_categories = new ArrayList<Integer>();
boolean[] itemsChecked = {false, false, false, false, false, false};
private FlatCheckBox fourniture,nourriture,voyages,habillement,medias,autres;
public interface dialoglistener {
public void onOkay(ArrayList<Integer> selected);
public void onCancel();
}
dialoglistener mlistener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// ensure that the host activity implements the callback interface
try {
// Instantiate the dialogListener so we can send events to the host
mlistener = (dialoglistener) activity;
} catch (ClassCastException e) {
// if activity doesn't implement the interface, throw an exception
throw new ClassCastException(activity.toString()
+ " must implement dialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View custom = inflater.inflate(R.layout.custom_settings,null);
fourniture = (FlatCheckBox)custom.findViewById(R.id.fourniture);
nourriture = (FlatCheckBox)custom.findViewById(R.id.nourriture);
voyages = (FlatCheckBox)custom.findViewById(R.id.voyages);
habillement = (FlatCheckBox)custom.findViewById(R.id.habillement);
medias = (FlatCheckBox)custom.findViewById(R.id.medias);
autres = (FlatCheckBox)custom.findViewById(R.id.autres);
if (selected_categories.contains(0)){
fourniture.setChecked(true);
}
if (selected_categories.contains(1)){
nourriture.setChecked(true);
}
if (selected_categories.contains(2)){
voyages.setChecked(true);
}
if (selected_categories.contains(3)){
habillement.setChecked(true);
}
if (selected_categories.contains(4)){
medias.setChecked(true);
}
if (selected_categories.contains(5)){
autres.setChecked(true);
}
builder.setView(custom)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
checkboxState();
mlistener.onOkay(selected_categories);
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mlistener.onCancel();
}
});
return builder.create();
}
public void checkboxState(){
if (fourniture.isChecked()){
if(!selected_categories.contains(0)){
selected_categories.add(0);
itemsChecked[0]=true;
}
else if (selected_categories.contains(0)) {
// Else, if the item is already in the array, remove it
selected_categories.remove(0);
itemsChecked[0]=false;
}
}
if (nourriture.isChecked()){
if(!selected_categories.contains(1)){
selected_categories.add(1);
itemsChecked[1]=true;
}
else if (selected_categories.contains(1)) {
selected_categories.remove(1);
itemsChecked[1]=false;
}
}
if (voyages.isChecked()){
if(!selected_categories.contains(2)){
selected_categories.add(2);
itemsChecked[2]=true;
}
else if (selected_categories.contains(2)) {
selected_categories.remove(2);
itemsChecked[2]=false;
}
}
if (habillement.isChecked()){
if(!selected_categories.contains(3)){
selected_categories.add(3);
itemsChecked[3]=true;
}
else if (selected_categories.contains(3)) {
selected_categories.remove(3);
itemsChecked[3]=false;
}
}
if (medias.isChecked()){
if(!selected_categories.contains(4)){
selected_categories.add(4);
itemsChecked[4]=true;
}
else if (selected_categories.contains(4)) {
selected_categories.remove(4);
itemsChecked[4]=false;
}
}
if (autres.isChecked()){
if(!selected_categories.contains(5)){
selected_categories.add(5);
itemsChecked[5]=true;
}
else if (selected_categories.contains(5)) {
selected_categories.remove(5);
itemsChecked[5]=false;
}
}
}
}
此代碼工作正常,但它看起來不是這樣!太多,如果elses ... 我的問題是,如果有一種方法來使用我的複選框的循環來獲得他們的狀態,然後將數組itemschecked
的元素設置爲真/假的持久性。 謝謝!
你能告訴我如何使用該數組來替換'if(!selected_categories.contains(0)){ selected_categories.add(0); itemsChecked [0] = true; (0)){ } else if(selected_categories.contains(0)){ selected_categories.remove(0); itemsChecked [0] = false; 'if(selected_categories.contains(0)){fourniture.setChecked(true);' – RidRoid 2014-12-05 09:18:07
1st,爲什麼需要有一個ArrayList和一個布爾值數組來保存跟蹤複選框的狀態? 2的1是足夠的國際海事組織。 – 2014-12-05 09:21:33
ArrayList將所選項目發送到主機活動,布爾值數組用於跟蹤狀態。這可能不是最好的辦法!我打開你的建議:) – RidRoid 2014-12-05 09:31:07