Android Studio does not recognize the getRating () method

0

When I try to use the getRating () method in the FormHelper class, Android Studio reports that "Can not resolve method 'getRating ()'".

I'm importing the View and RatingBar package, and Android Studio does not recognize this method anyway.

What should I do to get the getRating () method to work?

Code

package com.alura.magnero2018.agendaalura;

import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;

import com.alura.magnero2018.agendaalura.FormularioActivity;
import com.alura.magnero2018.agendaalura.R;


import alura.modelo.Aluno;

public class FormularioHelper extends View
{
    private EditText campoNome;
    private EditText campoEndereco;
    private EditText campoSitesPessoais;

    public EditText getCampoNome() {
        return campoNome;
    }

    public void setCampoNome(EditText campoNome) {
        this.campoNome = campoNome;
    }

    public EditText getCampoEndereco() {
        return campoEndereco;
    }

    public void setCampoEndereco(EditText campoEndereco) {
        this.campoEndereco = campoEndereco;
    }

    public EditText getCampoSitesPessoais() {
        return campoSitesPessoais;
    }

    public void setCampoSitesPessoais(EditText campoSitesPessoais) {
        this.campoSitesPessoais = campoSitesPessoais;
    }

    public EditText getCampoTelefone() {
        return campoTelefone;
    }

    public void setCampoTelefone(EditText campoTelefone) {
        this.campoTelefone = campoTelefone;
    }

    public EditText getCampoNotas() {
        return campoNotas;
    }

    public void setCampoNotas(EditText campoNotas) {
        this.campoNotas = campoNotas;
    }

    private EditText campoTelefone;
    private EditText campoNotas;

    public FormularioHelper(FormularioActivity activity)
    {
        EditText campoNome = activity.findViewById(R.id.nome);
        EditText campoEndereco = activity.findViewById(R.id.endereco);
        EditText campoSitesPessoais = activity.findViewById(R.id.sitesPessoais);
        EditText campoTelefone = activity.findViewById(R.id.telefone);
        RatingBar campoNotas = (RatingBar) activity.findViewById(R.id.notas);
    }

    public Aluno pegarAluno()
    {
        Aluno aluno = new Aluno();
        aluno.setNome(String.valueOf(campoNome.getText()));
        aluno.setEndereco(String.valueOf(campoEndereco.getText()));
        aluno.setSite(String.valueOf(campoSitesPessoais.getText()));
        aluno.setTelefone(String.valueOf(campoTelefone.getText()));
        aluno.setNota(Double.valueOf(campoNotas.getRating()));

        return aluno;
    }
}
    
asked by anonymous 17.12.2018 / 14:38

2 answers

0

My response is based on the codes you posted. Maybe the solution I'm going to present is not the most appropriate one.

Analysis

You have an attribute of FormularioHelper of type EditText called campoNotas :

private EditText campoNotas;

In your constructor, you probably try to set this attribute to be used later:

RatingBar campoNotas = (RatingBar) activity.findViewById(R.id.notas);

However, above, what you created was a new variable. So, then you try to use pegarAluno as if it were a RatingBar . But he is not. It remains EditText . Then the error is accused because the function getRating in EditText does not exist.

 aluno.setNota(Double.valueOf(campoNotas.getRating()));

Solution

You can create another attribute with another name and build it:

private RatingBar campoNotasRating;

public FormularioHelper(FormularioActivity activity)
{
    // aqui estou setando um valor para o atributo
    this.campoNotasRating = (RatingBar) activity.findViewById(R.id.notas);
}

And then use it:

 aluno.setNota(Double.valueOf(this.campoNotasRating.getRating()));
    
17.12.2018 / 15:17
1

Running code

package com.alura.magnero2018.agendaalura;

import android.widget.EditText;
import android.widget.RatingBar;

import com.alura.magnero2018.agendaalura.FormularioActivity;
import com.alura.magnero2018.agendaalura.R;


import alura.modelo.Aluno;

public class FormularioHelper 
{
    private EditText campoNome;
    private EditText campoEndereco;
    private EditText campoSitesPessoais;
    private EditText campoTelefone;
    private RatingBar campoNotas;

    public EditText getCampoNome() {
        return campoNome;
    }

    public void setCampoNome(EditText campoNome) {
        this.campoNome = campoNome;
    }

    public EditText getCampoEndereco() {
        return campoEndereco;
    }

    public void setCampoEndereco(EditText campoEndereco) {
        this.campoEndereco = campoEndereco;
    }

    public EditText getCampoSitesPessoais() {
        return campoSitesPessoais;
    }

    public void setCampoSitesPessoais(EditText campoSitesPessoais) {
        this.campoSitesPessoais = campoSitesPessoais;
    }

    public EditText getCampoTelefone() {
        return campoTelefone;
    }

    public void setCampoTelefone(EditText campoTelefone) {
        this.campoTelefone = campoTelefone;
    }

    public RatingBar getCampoNotas() {
        return campoNotas;
    }

    public void setCampoNotas(RatingBar campoNotas) {
        this.campoNotas = campoNotas;
    }


    public FormularioHelper(FormularioActivity activity)
    {
        EditText campoNome = activity.findViewById(R.id.nome);
        EditText campoEndereco = activity.findViewById(R.id.endereco);
        EditText campoSitesPessoais = activity.findViewById(R.id.sitesPessoais);
        EditText campoTelefone = activity.findViewById(R.id.telefone);
        RatingBar campoNotas = (RatingBar) activity.findViewById(R.id.notas);
    }

    public Aluno pegarAluno()
    {
        Aluno aluno = new Aluno();
        aluno.setNome(String.valueOf(campoNome.getText()));
        aluno.setEndereco(String.valueOf(campoEndereco.getText()));
        aluno.setSite(String.valueOf(campoSitesPessoais.getText()));
        aluno.setTelefone(String.valueOf(campoTelefone.getText()));
        aluno.setNota(Double.valueOf(campoNotas.getProgress()));

        return aluno;
    }
}
    
17.12.2018 / 15:39