ArrayList doesn't detect first input


Ask by : user1460211 November 03, 2012 23:41

I have an ArrayList in which the input of dynamically added EditTexts are referenced. I iterate the ArrayList so that I can get all the values of the EditTexts and add them together. However, I do have one non-dynamically added EditText that is already laid out in my layout.xml.

The problem is that whenever I click my Calculate Button and the only EditText on the screen is the already laid out EditText and it has an input of 1 or any other number, my Calculate Button shows the total input as 0. Whenever I add 1 or more editTexts dynamically, the total input includes the already laid out EditText after I click the Calculate Button.

I need to get the correct total input even if the only input is the non-dynamic 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();
View original question

Answer by : EmileNovember 03, 2012 19:23

I think you need to take a look at where your adding the tempText2 editText to your list. Check that it isn't as a result of adding the dynamic editTexts.

Look at where you add the following

allEd.add(tempText2);
allEd.add(editText2);

It could be that your only adding the tempText2 as a result of adding a dynamic field. As such when you don't add a dynamic field the arraylist will be empty. resulting in 0. When you do add a dynamic field this will result in the calculation working.

Difficult to say this is it without more code.

View original answer