プログラムでは、データを入力するときに入力する3つのフィールドがあり、下のボタンを押して開始すると、3つの動的入力フィールドが作成され、プログラムは上位3つのフィールドのみの平均スコアを計算します。ユーザーが動的に作成されたフィールドのデータを変更したときに、その数に関係なく、これらのフィールドからのデータをカウントしたい
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);