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.
精彩评论