DocumentReference docRef7 = FirebaseFirestore.getInstance().collection("admin").document("users_profile").collection("dynamic_profile").document("city");
docRef7.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
//-----------code for loading array item from firebase to spinner view-----------------
final List<String> group = (List<String>) document.get("options");
Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
cityFAdapter = new ArrayAdapter<String>(AdminSetting.this, android.R.layout.simple_spinner_item, group);
cityFAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityFSpinner.setAdapter(cityFAdapter);
cityFSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(AdminSetting.this);
AlertDialog.Builder builder= mBuilder.setTitle("Select your favourite 3 cities ");
mBuilder.setMultiChoiceItems(new String[]{String.valueOf(group)}, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int position, boolean isChecked) {
if (isChecked){
if (!mUserItems.contains(position)){
mUserItems.add(position);
}else {
mUserItems.remove(position);
}
}
if(isChecked){
mUserItems.add(position);
}else{
mUserItems.remove((Integer.valueOf(position)));
}
}
});
mBuilder.setCancelable(false);
mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String scity = "";
for (int i=0 ;i<mUserItems.size();i++){
scity = scity + group.get(mUserItems.get(i));
if (i != mUserItems.size() -1){
scity = scity+ ",";
}
}
displayCity.setText(scity);
}
});
mBuilder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
mBuilder.setNeutralButton("Clear all", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0; i<checkedItems.length;i++){
checkedItems[i]=false;
mUserItems.clear();
displayCity.setText("");
}
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
我想以列表方式显示带有对话框的警报对话框,而不是像图像中那样显示所有值。 因为我想从该列表中选择三个首选城市并将其显示在textview上。 以适当的方式设置适配器后,我会在微调器上获得列表,但我希望在带复选框的alertdialog上显示相同的内容
答案 0 :(得分:0)
需要在build.gradle文件中添加材质对话框的依赖项。
dependencies {
implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'
}
//实现以下代码。
new MaterialDialog.Builder(this)
.title(R.string.title)
.items(R.array.items)
.itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
/**
* If you use alwaysCallMultiChoiceCallback(), which is discussed below,
* returning false here won't allow the newly selected check box to actually be selected
* (or the newly unselected check box to be unchecked).
* See the limited multi choice dialog example in the sample project for details.
**/
return true;
}
})
.positiveText(R.string.choose)
.show();