1

The tutorial was from Droid Guru https://www.youtube.com/watch?v=DETCfQ_EOXo

  1. I wanted to get the sum of all numbers in edit text through iteration and display it on another activity.

'''

private boolean checkIfValidAndRead() {
    budgetInfos.clear();
    boolean result = true;

    for (int i = 0; i < layoutList.getChildCount(); i++) {

        View itemView = layoutList.getChildAt( i );
        EditText editItemName = (EditText) itemView.findViewById( R.id.budget_edittext_item );
        EditText editPrice = (EditText) itemView.findViewById( R.id.budget_edittext_price );
        
        BudgetInfo budgetInfo = new BudgetInfo();

        if (!editItemName.getText().toString().equals( "" )) {
            budgetInfo.setItem( editItemName.getText().toString() );
        } else {
            result = false;
            break;
        }
        if (!editPrice.getText().toString().equals( "" )) {
            budgetInfo.setItem( editPrice.getText().toString() );
        } else {
            result = false;
            break;
        }

        budgetInfos.add( budgetInfo );

        
    }
    if (budgetInfos.size() == 0) {
        result = false;
        Toast.makeText( this, "Add an item first.", Toast.LENGTH_SHORT ).show();
    } else if (!result) {
        Toast.makeText( this, "Enter all blank fields correctly.", Toast.LENGTH_SHORT ).show();
    }
    return result;
}

enter image description here

Thank you Sai H!!! I used the given code to my second activity from the comment.

//Passed the values through intent from main activity to second activity
budgetInfos = (ArrayList<BudgetInfo>) getIntent().getExtras().getSerializable( "list" );

        int total = 0;
        for (BudgetInfo price : budgetInfos) {
            total += price.getPrice();
            textView.setText( "Total Budget: " + total );}
  
`
Joseph
  • 21
  • 4
  • Does this answer your question? [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – a_local_nobody Jul 01 '21 at 18:21

2 Answers2

2

From the look of it, I don't think you need to get int values from EditTexts because all your values are in budgetInfo instances. If you just create an ArrayList<budgetInfo>, you can loop through the ArrayList to find the sum and add the result in your Intent to start another Activity.

Sai H.
  • 66
  • 5
  • 1
    Thank you for the response! I'm really new to android studio. I already have an ArrayList before the onCreate, `ArrayList budgetInfos = new ArrayList<>();` How can I loop through the arraylist to find the sum? On the code below `boolean result= true;` I added `int sum = 0` and below the EditText editPrice, I added: `int total = new Integer( String.valueOf( editPrice ) ).intValue(); sum += total;` – Joseph Jul 01 '21 at 19:39
  • 1
    You are still trying to get your value from EditText component, I suggest you take the values directly from your ArrayList. It should look something like this ... ```ArrayList myList = new ArrayList<>(); int total = 0; for (BudgetInfo price : myList) { total += price.getPrice(); } ``` – Sai H. Jul 02 '21 at 08:16
  • I would suggest that you dedicate time to work on your Java or Kotlin if you are getting into Android Development, also, do not forget Object Oriented Programming concepts, they are really important moving forward. When you become very comfortable and proficient with programming and writing codes, your Android journey will become 10x more enjoyable. Just some advice from my personal experience. Happy Coding! – Sai H. Jul 02 '21 at 08:27
  • 1
    Thank you for the advice and I will surely dedicate my time on learning the language and its concepts! I'll try to implement the code you have provided and get back for some updates. – Joseph Jul 02 '21 at 13:36
  • 1
    Thanks man! Really appreciated the help! I edited the post so that you can see the result. – Joseph Jul 02 '21 at 17:33
  • I'm glad I was able to help. Best of luck in your journey!! – Sai H. Jul 02 '21 at 18:34
1

Just store the value in a variable and pass the variable to the next activity using intent extra.

            Intent i = new Intent(MainActivity.this, NextActivity.class);
            i.putExtra("total", budgetInfo);
            startActivity(i);

And access the value from the next activity as :

 int total = getIntent().getIntExtra("total");
Rahul
  • 496
  • 4
  • 9
  • Thank you! But how would if store the values in a variable if there is no fix number of edit texts. The activity allows the user to insert a LinearLayout that includes 2 EditTexts: 1) Edit Text - text input 2) numberDecimal input. – Joseph Jul 01 '21 at 19:51
  • @Joseph so there are multiple edit text and the quantity of edittext changes dynamically? – Rahul Jul 02 '21 at 11:34
  • Yes there are 2 edit texts and when the user clicks on a floating action button (add button) it add another 2 edit texts. – Joseph Jul 02 '21 at 13:31