Android Studio does not recognize the getRating() method

Asked

Viewed 69 times

0

When I try to use the getRating() method in the Formulariohelper class Android Studio informs that "Cannot resolve method 'getRating()'".

I’m importing the package from View and Ratingbar and yet Android Studio does not recognize this method.

What should I do to make the getRating() method 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;
    }
}
  • The variable campoNotas is a view Ratingbar ?

  • 1

    I think you better put the full code. From Activity or Fragment that makes use of this view

  • The variable fieldNotas is a Ratingbar view.

2 answers

1

Code working

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

0


My answer is based on the codes you posted. Perhaps, the solution I will present is not the most appropriate.

Analyzing

You have an attribute of FormularioHelper of the kind 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. Then, after you try to use pegarAluno as if it were a RatingBar. But he is not. He remains one EditText. Soon the error is accused because the function getRating in the EditText there is no.

 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()));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.