Created
July 8, 2017 19:37
-
-
Save francisnnumbi/8924c0935c2ccdf310af51aef4f0b250 to your computer and use it in GitHub Desktop.
demonstrating a dialog with a list of item to select from. this list is passed from the constructor during initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.*; | |
import android.content.*; | |
import android.os.*; | |
import java.util.*; | |
public class ListDialog extends DialogFragment { | |
private String title; | |
private String[] choices; | |
private OnDismissListener listener; | |
private FragmentManager fragmentManager; | |
public ListDialog(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 | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setTitle(title) | |
.setItems(choices, new DialogInterface.OnClickListener(){ | |
@Override | |
public void onClick(DialogInterface p1, int p2) { | |
// TODO: Implement this method | |
listener.onDismiss(choices[p2]); | |
} | |
}) | |
.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(ListDialog.OnDismissListener listener) { | |
this.listener = listener; | |
} | |
// listener interface | |
public interface OnDismissListener { | |
public void onDismiss(String choice); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment