mercredi 14 janvier 2015

Set different default checked/unchecked on AlertDialog

I have a program that will pass a HashMap(Let say groupIdBefore) to a new Activity,


And in this Activity I will get some data from Database and stored it in HashMap (ListGroupId )


Then,


I want to create a checkbox alertDialog, this AlertDialog will shows some data from ListGroupId, then I want to compare ListGroupId and groupIdBefore, if value of ListGroupId is equal with groupIdBefore,


Then (when the checkbox AlertDialog is shown) the checkbox is set Checked, otherwise the checkbox is left unchecked.


Here are my code,



public void alertDialogBuilder() {
// TODO Auto-generated method stub
Log.d("Flag", "arrive at alertdialogBuilder");
final String[] listGroupName = new String[mGroupList.size()];
final String[] listGroupId = new String[mGroupList.size()];
final boolean[] itemsChecked = new boolean[mGroupList.size()];

final String[] groupIdBefore = new String[localGroupList.size()];

// Get hashmap from previous Activity and convert to string
for (int i = 0; i < localGroupList.size(); i++) {
groupIdBefore[i] = localGroupList.get(i).get("groupId");
}

// Convert all groupId and groupName produced by this activity (all
// groups this User has)
for (int i = 0; i < mGroupList.size(); i++) {
Dat[i] = mGroupList.get(i).get("groupName");
listGroupId[i] = mGroupList.get(i).get("groupId");

for (int j = 0; j < localGroupList.size(); j++) {
if (listGroupId[i].equals(groupIdBefore[j])) {
itemsChecked[i] = true;
}
}

}

AlertDialog.Builder builder = new AlertDialog.Builder(
EditAnnouncement.this);

builder.setTitle("Choose Your Group : ");

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
groupList.clear();
stringList.clear();
selectedGroups.setText("Post To : ");

for (int i = 0; i < listGroupName.length; i++) {
if (itemsChecked[i]) {
groupList.add(listGroupName[i]);
stringList.add(listGroupId[i]);
itemsChecked[i] = false;
}
Log.d("flag", "after for");

}
String string = "";
for (int i = 0; i < groupList.size(); i++) {
selectedGroups.setText("Post To : ");
string = string + " " + groupList.get(i);

}
selectedGroups.setText(selectedGroups.getText().toString()
+ string);
}
});
builder.setMultiChoiceItems(listGroupName, new boolean[] { false,
false, false },
new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
itemsChecked[which] = isChecked;
}
});
builder.show();
Log.d("flag", "bottom of alert dialog");
}


But items with the same value is not checked,


Can you please help me to solve this problem? I dont really understand how checkbox alertdialog really works


Thank you in advance





Aucun commentaire:

Enregistrer un commentaire