2013-02-07 53 views
0

當我輸入數據並按下按鈕以啓動下面的按鈕時,我有3個字段創建三個動態輸入字段,程序計算只有3個頂部字段的平均分數。我想,當用戶改變了動態創建的字段中的任何數據從這些字段數的數據,而不管其數量在程序中從動態創建的EditText中獲取文本

package com.example.averegemark; 

    import android.app.Activity; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.LinearLayout; 
    import android.widget.TableLayout; 
    import android.widget.TableRow; 
    import android.widget.TableRow.LayoutParams; 
    import android.widget.TextView; 
    import android.widget.Toast; 

public class Table extends Activity { 

    LinearLayout LL; 
    EditText et1,et2,et3; 
    double coef=0,points=0,sum=0,coefsum=0; 
    TextView total; 
    Button bNext; 
    String name="",mark="",coeff=""; 
    int i=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.table); 
     bNext = (Button) findViewById(R.id.bNext); 
     et1 = (EditText) findViewById(R.id.et1); 
     et2 = (EditText) findViewById(R.id.et2); 
     et3 = (EditText) findViewById(R.id.et3); 
     total = (TextView) findViewById(R.id.tvTotal); 
     total.setText("GPA:"); 
     LL = (LinearLayout) findViewById(R.id.LL); 

     bNext.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       name = et1.getText().toString(); 
       mark = et2.getText().toString(); 
       coeff = et3.getText().toString(); 
       if(mark.length()==0 || coeff.length()==0) 
        Toast.makeText(getApplicationContext(), "Enter mark `and coefficient", Toast.LENGTH_LONG).show(); 
       else 
        NewView(name,mark,coeff); 
      } 
     }); 

    } 

    private void NewView(String name, String mark, String coeff) { 
     // TODO Auto-generated method stub 
     EditText info1 = new EditText(this); 
     EditText info2 = new EditText(this); 
     EditText info3 = new EditText(this); 

     info1.setId(i);i++; 
     info1.setId(i);i++; 
     info1.setId(i);i++; 

     info1.setText(""+name); 
     info2.setText(""+mark); 
     info3.setText(""+coeff); 


     TableLayout table = (TableLayout) findViewById(R.id.tbl); 
     table.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     TableRow row = new TableRow(this); 
     row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     row.addView(info1); 
     row.addView(info2); 
     row.addView(info3); 

     table.addView(row); 


     String rez=""; 
     rez= info2.getText().toString(); 
     points = Double.parseDouble(rez); 
     rez= info3.getText().toString(); 
     coef = Double.parseDouble(rez); 
     sum +=coef*points; 
     coefsum+=coef; 
     sum/=coefsum; 
     total.setText("GPA : "+sum+"\nCoefficient sum. "+coefsum); 

     et3.setText(""); 
     et2.setText(""); 
     et1.setText(""); 
     et1.requestFocus(); 
    } 
} 

所以當我將行更改任何數據,我想重新計算這一點,但在這裏它只使用頂部EditText不動態創建:

  String rez=""; 
     rez= info2.getText().toString(); 
     points = Double.parseDouble(rez); 
     rez= info3.getText().toString(); 
     coef = Double.parseDouble(rez); 
     sum +=coef*points; 
     coefsum+=coef; 
     sum/=coefsum; 
     total.setText("GPA : "+sum+"\nCoefficient sum. "+coefsum); 
+0

我現在讀了5次,我不明白。也許問題出在我身上,但如果你重新解釋你的問題,這可能會有所幫助。 – stpn108

+0

OKEY,你會看到,在佈局中創建了3個EditText,每次當你輸入數據並按下按鈕時,它們下面會用相同的數據創建3個新的EditText。程序計算GPA =係數*標記\係數和; 我希望程序在用戶每次更改動態創建的EditText中的某些數據時重新計算GPA。 對不起,對我來說很難正確地提出問題。 – CVS

+0

私人無效getCount將(){ \t \t \t \t TableLayout表=(TableLayout)findViewById(R.id.tbl); \t \t \t \t ArrayList metl = new ArrayList (); \t \t \t \t嘗試{ \t \t對(INT I = 0; I CVS

回答

0

當您按下按鈕,您從原始edittexts中獲取信息。你必須找到表格中的最後一行並從那裏獲取edittext。

相關問題