[英]android.content.res.Resources$NotFoundException when setting a ListView
[英]Android: setting ListView as Dialog content programatically
我究竟做錯了什么? 我得到一個沒有內容的空白對話框。 代碼:
@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.addFooterView(layoutNormal);
layout.addView(modeList);
dialog.setContentView(layout);
return dialog;
}
[...]
解決方案:
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();
但老實說,使用 AlertDialog.Builder.setItems() 是一個更好的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.