Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:33:52+00:00 2026-06-13T17:33:52+00:00

I have an ArrayList in which the input of dynamically added EditTexts are referenced.

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:33:53+00:00Added an answer on June 13, 2026 at 5:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an arraylist called fetchContactName() which returns all names (31) in my contact
I have an input signal that I want to store in an ArrayList then
I have a problem with ArrayList in C#, I know how can I add
At the moment inside the class I have ArrayList which the objects are stored
I have an arraylist which has a class datastructure. public class empRec { public
i have an arraylist of strings in my servlet which im passing forwarding using
There is a JSP page in which i have an ArrayList variable. I need
Ok, so I have an ArrayList (arrBok) , which is full of book objects
So I have this method which will take an ArrayList of Integers, I compare
I have a EditText box which is used to input names. Once the names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.