Как автоматически обновлять расчеты в TextViews? - PullRequest
0 голосов
/ 21 апреля 2020

В моем коде я выполняю некоторые вычисления и хотел бы узнать, как я могу обновить TextView, который отображает результат в режиме реального времени?

Нужно ли использовать что-то вроде метода onDataChange? как то так?

@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String appTitle = dataSnapshot.getValue().toString();
            Log.e("Hey", appTitle);
            title.setText(appTitle);
        }

Это мой код:

public class Activity6 extends AppCompatActivity {

EditText editTextHouse;
EditText editTextFood;    
TextView textViewTotalMonthlyAverage;
Button buttonLoadM7;


public static int houseValue;
public static int foodValue;    
public static int monthlyAverageValue;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_6);

    editTextHouse = (EditText) findViewById(R.id.editTextHouse);
    editTextFood = (EditText) findViewById(R.id.editTextFood);        
    textViewTotalMonthlyAverage = (TextView) findViewById(R.id.textViewTotalMonthlyAverageValue);

    buttonLoadM7 = (Button) findViewById(R.id.buttonLoadM7);  

        try {
            houseValue = Integer.parseInt(editTextHouse.getText().toString());
            foodValue = Integer.parseInt(editTextFood.getText().toString());
            utilitiesValue = Integer.parseInt(editTextUtilities.getText().toString());
            transportationValue = Integer.parseInt(editTextTransportation.getText().toString());
            insuranceValue = Integer.parseInt(editTextInsurance.getText().toString());
            clothingValue = Integer.parseInt(editTextClothing.getText().toString());
            holidaysValue = Integer.parseInt(editTextHolidays.getText().toString());
            entertainmentValue = Integer.parseInt(editTextEntertainment.getText().toString());
            educationValue = Integer.parseInt(editTextEducation.getText().toString());
            vehiclesValue = Integer.parseInt(editTextVehicles.getText().toString());
            othersValue = Integer.parseInt(editTextOthers.getText().toString());
            monthlyAverageValue = houseValue + foodValue + utilitiesValue + transportationValue + insuranceValue + clothingValue
                    + holidaysValue + entertainmentValue + educationValue + vehiclesValue + othersValue;
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }

        editTextHouse.setText(String.valueOf(houseValue));
        editTextFood.setText(String.valueOf(foodValue));       
        textViewTotalMonthlyAverage.setText(String.valueOf(monthlyAverageValue));


    buttonLoadM7.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent loadM7 = new Intent(Activity6.this, Activity7.class);
            startActivity(loadM7);
        }
    });

}

Заранее спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...