How do I set a TextView with a DOUBLE variable?

0

I'm trying to set the variable txtMedia1 to the value of the variable media1 . I did this, but Android Studio indicates that I have to change txtMedia1 to double or media1 to TextView :

package com.app.jeffersonalencar.composicao;

import android.renderscript.Double2;
import android.renderscript.Sampler;
import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;

public class act_mains extends AppCompatActivity implements View.OnClickListener{

    //Declarando variáveis de coleta de medida
    public EditText edtTxt1, edtTxt2, edtTxt3;
    //Declarando campo de visualização da média por parte
    public TextView txtMedia1;
    //Declarando botão
    public Button btnMedia1;


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

        //Setando os valores dos campos de coleta para as variáveis
        edtTxt1 = (EditText) findViewById(R.id.edtTxt1);
        edtTxt2 = (EditText) findViewById(R.id.edtTxt2);
        edtTxt3 = (EditText) findViewById(R.id.edtTxt3);


    }

    public void onClick(View V){
        double medida1 = Double.parseDouble(edtTxt1.getText().toString());
        double medida2 = Double.parseDouble(edtTxt2.getText().toString());
        double medida3 = Double.parseDouble(edtTxt3.getText().toString());


        double media1 = (medida1 + medida2 + medida3)/3;

        //Terminar a briga e colocar o valor no TextView
        String media2 = String.valuesOf(media1);
        txtMedia1 = (TextView) getText(R.id.media2) ;

    }


}

Realizing what Android Studio still asks for error.

    
asked by anonymous 24.01.2017 / 17:19

2 answers

0

There are several ways you can do this:

Form 1

String valueDouble= Double.toString(media1);
txtMedia1.setText(valueDouble);

Form 2

txtMedia1.setText(""+media1);

Form 3

txtMedia1.setText(String.valueOf(media1));

Form 4

Here it would be basically the same as Form 3, where you can use format to set the format you want, depending on how many houses you display after the comma. See:

txtMedia1.setText(String.format("%.2f", value));
    
24.01.2017 / 17:30
1

I use a similar conversion in an app that has been developed. Fitting for your example would look like this:

txtMedia.setText(Double.toString(media1));
    
24.01.2017 / 17:43