Reputation: 15
i was asked to design an app in android.
we have to take input of 3 numeric values and show some calculations in summary page.
i am able to do a simple application by taking the input details and passing them using bundles to summary page and doing calculations of average. (1 time entry information). but now, we want to add multiple times and summary need to give average of that. Please help me with this.
Upvotes: 0
Views: 1588
Reputation: 681
Storing multiple values
If you take a look at the documentation for the Intent class, you can see that you can store an arraylist of integers using the putIntegerArrayListExtra method. You can then extract the arraylist using getIntegerArrayListExtra This should make it pretty trivial to store multiple integers without doing string conversion and without really using bundles
Examples
Storing the ArrayList in the bundle
Intent intent = new Intent(this, OtherActivity.class);
ArrayList<Integer> values = new ArrayList<>();
// Add values to array list
intent.putIntegerArrayListExtra("myKey", values);
Extracting the ArrayList from the Bundle
Intent intent = getIntent();
List<Integer> values = intent.getIntegerArrayListExtra("myKey");
Communicating between two activities
Assuming you have two avtivities called InputActivity and CalculateActivity you can do something like this. For more info on using and handling startActivityForResult check out THIS question.
public class InputActivity extends Activity {
// NOTE: Make sure this gets initialized at some point
private ArrayList<Integer> pastValues;
private void doSomethingWithInput(int input) {
Intent intent = new Intent(this, CalculateActivity.class);
this.pastValues.add(input)
intent.putIntegerArrayListExtra("values", this.pastValues);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
// Here is the result. Do whatever you need with it.
int result = data.getIntExtra("result");
}
}
}
public class CalculateActivity extends Activity {
private void doCalculation() {
Intent intent = this.getIntent();
List<Integer> values = intent.getIntegerArrayListExtra("values");
// do something with the extracted values
int result = ....;
Intent returnIntent = new Intent();
intent.putExtra("result", result);
this.setResult(RESULT_OK, returnIntent);
this.finish();
}
}
Upvotes: 1