开发者

Android: setting ListView as Dialog content programatically

what am i doing wrong? i'm getting a blank dialog with no content. code:

@Override
protected Dialog onCreateDialog(int id, Bundle b) {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    switch(id) {
        case DIALOG_COLOR_MODE:
        {
            LinearLayout layout = new LinearLayout(this);
            LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.setLayoutParams(lllp);

            ListView modeList = new ListView(this);
            Dialog dialog = new Dialog(this);

            TextView layoutBright = new TextView(this);
            TextView layoutNormal = new TextView(this);

            layoutBright.setText("Bright Mode");
            layoutNormal.setText("Normal Mode");

            modeList.addFooterView(layoutBright);
            modeList.a开发者_StackOverflow社区ddFooterView(layoutNormal);

            layout.addView(modeList);

            dialog.setContentView(layout);

            return dialog;
        }
[...]


solution:

Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
dialog = builder.create();

but to be honest, using AlertDialog.Builder.setItems() is a much better solution for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜