在 textView 中添加来自另一个 class 的整数

Add integer from another class in a textView

MainActivity class 中,我有一个 TextView,我想用另一个 [=27] 的 整数 填充它=].整数是一些数据库计算的结果。我怎样才能用另一个 class 的 'sum' 整数设置 TextView 文本?谢谢!

MainActivity TextView:

valFormula.setText( here I need to pass the sum Integer);

这是我计算总和值的class:

public class CalculateItems extends AppCompatActivity {
        MyDBHandler myDb;
        String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        int sum;

    public void myResult() {
                List<Integer> items = new ArrayList<>();
                myDb = new MyDBHandler(this);
                Cursor cursor = myDb.getItemsToday();
                if (cursor !=null) {
                        while (cursor.moveToNext()) {
                                int timeWhenAdded = cursor.getInt(1);
                                int timePassed = Integer.parseInt(timeStamp) - timeWhenAdded;
                                int price = total - (int) ((percentageToDecrease / 100) * discount);
                                items.add(price);
                        }
                }
                //This is the variable that I want to have it displayed in the textView:
                sum = 0;
                for (int i : items)
                        sum += i;
        }

通常您不应更新另一个 activity 的视图。如果你只需要计算一些东西创建静态方法

public class CalculateItems {
    public static int myResult() {
        int sum = 0;
        List<Integer> items = new ArrayList<>();
        MyDBHandler myDb = new MyDBHandler(this);
        Cursor cursor = myDb.getItemsToday();
        if (cursor !=null) {
            while (cursor.moveToNext()) {
                int timeWhenAdded = cursor.getInt(1);
                int timePassed = Integer.parseInt(timeStamp) - timeWhenAdded;
                int price = total - (int) ((percentageToDecrease / 100) * discount);
                items.add(price);
            }
        }
        for (int i : items)
            sum += i;
        return sum;
    }
}

并在需要时从主 activity 调用它

valFormula.setText(CalculateItems.myResult());