Skip to content

Instantly share code, notes, and snippets.

@francisnnumbi
Created July 8, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisnnumbi/1221aed9e5449991e288bae4fe5a3d74 to your computer and use it in GitHub Desktop.
Save francisnnumbi/1221aed9e5449991e288bae4fe5a3d74 to your computer and use it in GitHub Desktop.
a multichoice dialog class
import android.app.*;
import android.content.*;
import android.os.*;
import java.util.*;
public class MultiChoiceListDialog extends DialogFragment {
private String title;
private String[] choices;
private ArrayList<String> selectedItems;
private OnDismissListener listener;
private FragmentManager fragmentManager;
public MultiChoiceListDialog(FragmentManager fragmentManager, String title, String[] choices) {
this.title = title;
this.fragmentManager = fragmentManager;
this.choices = choices;
listener = null;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO: Implement this method
selectedItems = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title)
.setMultiChoiceItems(choices, null, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface p1, int p2, boolean p3) {
// TODO: Implement this method
if(p3){
selectedItems.add(choices[p2]);
}else if(selectedItems.contains(choices[p2])){
selectedItems.remove(choices[p2]);
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface p1, int p2) {
// TODO: Implement this method
listener.onDismiss(selectedItems);
}
})
.setNeutralButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface p1, int p2) {
// TODO: Implement this method
listener.onDismiss(null);
}
});
return builder.create();
}
public void show() {
show(fragmentManager, "");
}
public void setOnDismissListener(MultiChoiceListDialog.OnDismissListener listener) {
this.listener = listener;
}
// listener interface
public interface OnDismissListener {
public void onDismiss(ArrayList<String> choice);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment