Android对话框显示空弹出窗口

时间:2014-12-07 03:06:58

标签: java android arrays dialog

基本上我试图将strings.xml中的数组填充到Android中的对话框中。这是我的strings.xml:

<string-array name="flavour_choice">
    <item>Banana</item>
    <item>Chocolate</item>
    <item>Mango</item>
    <item>Raspberry</item>
    <item>Strawberry</item>
    <item>Vanila</item>
</string-array>

填写对话框的代码:

static int FLAVOUR_CHOICE = 1;
static String[] mFlavourArray;
static String flavourSelected;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFlavourArray = getResources().getStringArray(R.array.flavour_choice);

    tvFlavour = (TextView) findViewById(R.id.tvFlavour);
    tvFlavour.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            MyFlavourDialog dialog = new MyFlavourDialog();
            dialog.setDialogType(FLAVOUR_CHOICE);
            dialog.show(getFragmentManager(), "MyFlavourDialog");
        }
    });
}

public static class MyFlavourDialog extends DialogFragment {
    int dialogType;
    public void setDialogType(int type){
        dialogType = type;
    }
    public Dialog onCreatDialog(Bundle savedInstance){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        if(dialogType == FLAVOUR_CHOICE){
            builder.setTitle("Ice Cream Flavour");
            builder.setItems(R.array.flavour_choice, new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    flavourSelected = mFlavourArray[which];
                }
            });
        }
        return builder.create();
    }
}

但是,当我在tvFlavour上选择时,会弹出一个没有标题或内容的空对话框。我想知道我错过了哪些部分。提前谢谢。

2 个答案:

答案 0 :(得分:1)

你拼错了onCreateDialog

答案 1 :(得分:0)

new AlertDialog.Builder(this)
.setTitle("click here")
.setMessage("click meesage .....")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // do somethig or  call method which u want 
    }
 })
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // do nothing
    }
 })
.setIcon(android.R.drawable.image1)
 .show();