2012-05-28 67 views
0

比方說,我有一些像這樣的公式列表視圖:如何在自定義對話框中動態添加組件?

((A * 2 + B * 3 + PF * 5)/10) 

當我點擊它,我想字母改爲EditTexts,所以我再拆這個String,如果它是一個字母我應該將其替換爲EditText,那麼,我應該使用什麼來實現這一點?我的意思是,我可以在「對話框樣式」窗口中加載一個活動嗎?或者我可以使用簡單對話框工作?

我已經試過這一點,但添加視圖(NullPointerException

時,得到錯誤
lsView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 

@Override 
public boolean onItemLongClick(AdapterView<?> aView, View view, 
      int i, long l) { 
    Dialog dialogFormula = new Dialog(NotasActivity.this); //"Parent" activity 

    dialogFormula.setTitle("Title"); 

    LinearLayout layout = (LinearLayout)findViewById(R.id.DialogLayout); 
    String[] lines = materias.get(i).getFormula().split(" "); 
    for (String s : lines) { 
     if (s.compareTo("PARTIC") == 0) { 
      EditText edtPartic = new EditText(dialogFormula.getContext)); 
      layout.addView(edtPartic); 
     } else if (s.contains("A") && s.compareTo("A") != 0) { 
     } else if (s.compareTo("B") == 0) { 
     } else if (s.compareTo("C") == 0) { 
     } else if (s.compareTo("D") == 0) { 
     } else if (s.compareTo("E") == 0) { 
     } else if (s.compareTo("F") == 0) { 
     } else if (s.compareTo("G") == 0) { 
     } else if (s.compareTo("H") == 0) { 
     } else if (s.compareTo("I") == 0) { 
     } else if (s.compareTo("PF") == 0) { 
    } 
       } 
    dialogFormula.setContentView(layout); 
    return false; 
} 
}); 

dialog_formula.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/DialogLayout"> 



<TextView 
    android:id="@+id/txtMateriaDialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 


<TextView 
    android:id="@+id/txtFormulaDialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

</LinearLayout> 
+0

凡佈局XML聲明? 另外,你有什麼錯誤?空指針? –

+0

用信息編輯 – Markissimo

+0

你想用這行做什麼:'LinearLayout layout =(LinearLayout)findViewById(R.id.DialogLayout);'? – Sam

回答

1

我建議你做一個自定義對話框類。

public class FormulaDialog extends Dialog { 

    public FormulaDialog(Context c) { 
     super(c); 
     setTitle("Title"); 
     setContentView(R.layout.dialog_formula); 
    } 

    public void updateLayout(String[] lines) { 
     LinearLayout layout = (LinearLayout)findViewById(R.id.DialogLayout); 
     for (String s : lines) { 
      if (s.compareTo("PARTIC") == 0) { 
       EditText edtPartic = new EditText(dialogFormula.getContext)); 
       layout.addView(edtPartic); 
      } else if (s.contains("A") && s.compareTo("A") != 0) { 
      } else if (s.compareTo("B") == 0) { 
      } else if (s.compareTo("C") == 0) { 
      } else if (s.compareTo("D") == 0) { 
      } else if (s.compareTo("E") == 0) { 
      } else if (s.compareTo("F") == 0) { 
      } else if (s.compareTo("G") == 0) { 
      } else if (s.compareTo("H") == 0) { 
      } else if (s.compareTo("I") == 0) { 
      } else if (s.compareTo("PF") == 0) { 
      } 
     } 
    } 
} 

然後在你的活動,所有您需要運行是

String[] lines = materias.get(i).getFormula().split(" "); 
FormulaDialog mFormulaDialog = new FormulaDialog(this); 
mFormulaDialog.updateLayout(lines); 
mFormulaDialog.show(); 
+0

謝謝,完美無缺! – Markissimo

0

findViewById()只傳遞給XML返回意見當前Activity的setContentView(),通常這是您的第一個活動的main.xml。由於DialogLayout是在不同的XML,這條線返回null:

LinearLayout layout = (LinearLayout)findViewById(R.id.DialogLayout); 

你要膨脹dialog_formula.xml,修改它,然後這個XML傳遞給你的對話框;像這樣:

View view = getLayoutInflater().inflate(R.layout.dialog_formula, null, false); 

LinearLayout layout = (LinearLayout) view.findViewById(R.id.DialogLayout); 
// Add your new EditTexts here 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Title"); 
builder.setView(view); 
dialog = builder.create(); 

爲了解析公式,我有兩個不同的建議:

  • 保持用戶變量的數量就各化學式,而不是遍地解析相同的字符串。
  • 如果你必須分析,測試你的字符串是這樣的:

    if("ABCDEFGHIPF".contains(s)) 
        // Add EditText 
    else if(s.contains("PARTIC")) 
        // Do something special 
    // The rest are numbers or symbols 
    
相關問題