I am trying to get a sum from all the user numbers input. User enter a quantity number from dialog. The quantity is the shown on a TextView, inside the ArrayList quantity. In the ArrayList quantity, i can get position(0) and position(1). But the idea is to ge all the Arraylist positions and esecute the sum of the numbers.
I am trying with this code, but obviously something is not right.
int total1 = Integer.parseInt(quantity.get(0));
int total2 = Integer.parseInt(quantity.get(1));
int total = 0;
for (int i = 0; i < quantity.size(); i++) {
total = total2 + total1 ;
}
System.out.println(Integer.valueOf(total));
This is the Activity
public class CalculatorActivity extends AppCompatActivity {
Button btnCclcola;
TextView cancel, doneBtn, nameTV, quantityTV, resulttry;
EditText ingrName, ingrQuantity;
Dialog dialog;
RecyclerView recyclerView;
ArrayList<String> text = new ArrayList<>();
ArrayList<String> quantity = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
nameTV = findViewById(R.id.nameTV);
quantityTV = findViewById(R.id.qunatityTV);
resulttry = findViewById(R.id.resulttry);
btnCclcola = findViewById(R.id.btn_calcola);
recyclerView = findViewById(R.id.rv_calculator);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
CalculatorAdapter calculatorAdapter = new CalculatorAdapter(this, text, quantity);
recyclerView.setAdapter(calculatorAdapter);
btnCclcola.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total1 = Integer.parseInt(quantity.get(0));
int total2 = Integer.parseInt(quantity.get(1));
int total = 0;
for (int i = 0; i < quantity.size(); i++) {
total = total2 + total1 ;
}
System.out.println(Integer.valueOf(total));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {...}
private void opendialog() {...}
private void validateCategory() {...}
private void saveInfo() {
String ingName = ingrName.getText().toString();
int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
text.add(ingName);
quantity.add(String.valueOf(Integer.valueOf(ingQuant)));
}