0

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.

4

1 回答 1

1

存储多个值

如果您查看Intent类的文档,您会发现可以使用putIntegerArrayListExtra方法存储整数数组列表。然后,您可以使用getIntegerArrayListExtra提取数组列表这应该使得在不进行字符串转换且不真正使用包的情况下存储多个整数变得非常简单

例子

将 ArrayList 存储在包中

Intent intent = new Intent(this, OtherActivity.class);
ArrayList<Integer> values = new ArrayList<>();
// Add values to array list
intent.putIntegerArrayListExtra("myKey", values);

从 Bundle 中提取 ArrayList

Intent intent = getIntent();
List<Integer> values = intent.getIntegerArrayListExtra("myKey");

在两个活动之间进行通信

假设您有两个名为 InputActivity 和 CalculateActivity 的活动,您可以执行类似的操作。有关使用和处理 startActivityForResult 的更多信息,请查看这个问题。

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();
    }
}
于 2015-05-03T17:56:03.020 回答