Добавить целое число из другого класса в textView

1

В классе MainActivity меня есть TextView и я хочу, чтобы он MainActivity целым числом из другого класса. Целое число является результатом некоторых вычислений базы данных. Как я могу установить текст TextView с целым числом sum из другого класса? Спасибо!

MainActivity TextView:

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

И это класс, в котором я вычисляю значение суммы:

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;
        }
Теги:
textview

1 ответ

1

Обычно вы не должны обновлять представления другого действия. Если вам нужно просто вычислить что-то, создайте статический метод

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;
    }
}

и звоните из основной деятельности, когда вам это нужно

valFormula.setText(CalculateItems.myResult());

Ещё вопросы

Сообщество Overcoder
Наверх
Меню