我有一个ArrayList,其中引用了动态添加的EditTexts的输入。我迭代ArrayList,以便我可以获取EditTexts的所有值并将它们一起添加。但是,我确实有一个已经在layout.xml中布局的非动态添加的EditText。
问题在于每当我点击我的计算按钮并且屏幕上唯一的EditText是已经布局的EditText并且输入为1或任何其他数字时,我的计算按钮显示总输入为0.每当我动态添加1个或多个editTexts,单击“计算”按钮后,总输入包括已布局的EditText。
即使唯一的输入是非动态editText,我也需要获得正确的总输入。
int count = 1;
double gradeValue;
List<EditText> allEd = new ArrayList<EditText>();
List<Spinner> allSp = new ArrayList<Spinner>();
EditText editText1;
EditText editText2;
EditText tempText1;
EditText tempText2;
Spinner spinner1;
Spinner spinnerTemp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonAdd = (Button) findViewById(R.id.button1);
Button buttonDel = (Button) findViewById(R.id.button2);
Button buttonCalc = (Button) findViewById(R.id.button3);
spinner1 = (Spinner) findViewById(R.id.spinner1);
String[] options = new String[13];
options[0] = "A+";
options[1] = "A";
options[2] = "A-";
options[3] = "B+";
options[4] = "B";
options[5] = "B-";
options[6] = "C+";
options[7] = "C";
options[8] = "C-";
options[9] = "D+";
options[10] = "D";
options[11] = "D-";
options[12] = "F";
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, options);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner1.setAdapter(spinnerArrayAdapter);
allEd.add(editText2);
allSp.add(spinner1);
buttonAdd.setOnClickListener(this);
buttonDel.setOnClickListener(this);
buttonCalc.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
public void onClick(View v) {
TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
switch(v.getId()){
case R.id.button1:
if(count != 16){
count++;
// Create the row only when the add button is clicked
TableRow tempRow = new TableRow(MainActivity.this);
EditText tempText1 = new EditText(MainActivity.this);
EditText tempText2 = new EditText(MainActivity.this);
TextView tempTextView = new TextView(MainActivity.this);
Spinner spinnerTemp = new Spinner(MainActivity.this);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
TextView textView3 = (TextView) findViewById(R.id.textView3);
tempTextView.setText(count + ".");
tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tempText1.setLayoutParams(editText1.getLayoutParams());
tempText2.setLayoutParams(editText2.getLayoutParams());
tempTextView.setLayoutParams(textView3.getLayoutParams());
tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
tempText2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
tempText2.setId(count);
spinnerTemp.setLayoutParams(spinner1.getLayoutParams());
spinnerTemp.setId(count);
String options[] = { "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" };
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, options);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinnerTemp.setAdapter(spinnerArrayAdapter);
allEd.add(tempText2);
allSp.add(spinnerTemp);
tempRow.addView(tempTextView);
tempRow.addView(tempText1);
tempRow.addView(tempText2);
tempRow.addView(spinnerTemp);
tableLayout1.addView(tempRow);
} else {
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Error");
alertDialog.setMessage("You can only have up to 16 rows!");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
break;
case R.id.button3:
int calculation = 0;
for(int i = 0; i < allEd.size(); i++) {
EditText totalUnits = allEd.get(i);
try {
int units = Integer.parseInt(totalUnits.getText().toString());
calculation += units;
}catch (Exception e) {
//ignore
}
}
double grade = 0;
for(int i = 0; i < allSp.size(); i++) {
double gradeValue = calcGradeValue(allSp.get(i).getSelectedItemPosition());
try {
double calculation1 = (gradeValue) * (Integer.parseInt(allEd.get(i).getText().toString()));
grade += calculation1;
}catch (Exception e) {
//ignore
}
}
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Your calculated GPA");
alertDialog.setMessage("Your calculated GPA is: " + (grade));
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
答案 0 :(得分:0)
我想也许你没有启动值editText2,并且你尝试将null对象添加到列表中,但是我真的不能说没有任何源代码。
答案 1 :(得分:0)
我认为您需要查看将tempText2 editText添加到列表的位置。检查它是否是添加动态editTexts的结果。
查看您添加以下内容的位置
allEd.add(tempText2);
allEd.add(editText2);
可能是您添加动态字段后只添加了tempText2。因此,当您不添加动态字段时,arraylist将为空。结果为0.当您添加动态字段时,这将导致计算工作。
很难说这是没有更多代码的。