How are these Setters methods not being used?

Asked

Viewed 125 times

-1

I’m creating a Java/Android app where your job is basically to display a list for the user about what items we still have available in stock.

To make this list I used a Recycler View, data (stock items) are manually added by me on Firebase, this in turn mirrors the data as a list (recyclerView) in my app.

So far everything has gone very well...

However, when running the app the following message appears in my Logcat:

W/ClassMapper: No setter/field for dT (difiteria e tétano) found on class com.imagine.edielsonpereira.cs_ivacinasstock.activity.modelagem.Vacina

I also realized that my methods Setters, in my modeling class, are not being used because they are faded, I assume the problem is there.

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Follow the warning (I wouldn’t say it’s a bug) in Logcat:

inserir a descrição da imagem aqui

Configuration of Realtime Database that communicates with my app:

inserir a descrição da imagem aqui

Follows the main class configuration:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Class Configuracaofirebase

package com.imagine.edielsonpereira.cs_ivacinasstock.activity.config;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class ConfiguracaoFirebase {

    private static DatabaseReference firebase;

    //Retorna Instancia do Firebase
    public static DatabaseReference getFirebaseDatabase(){
        if(firebase == null){
            firebase = FirebaseDatabase.getInstance().getReference();
        }
        return firebase;
    }

}

Adapter:

package com.imagine.edielsonpereira.cs_ivacinasstock.activity.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.imagine.edielsonpereira.cs_ivacinasstock.R;
import com.imagine.edielsonpereira.cs_ivacinasstock.activity.modelagem.Vacina;

import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    private List<Vacina> listaVacinas;
    Context context;

    public Adapter(List<Vacina> listaVacinas, Context context) {
        this.listaVacinas = listaVacinas;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        //Converte o objeto do tipo XML em uma View.
        View itemLista = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_lista, viewGroup, false);
        return new MyViewHolder(itemLista);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

        Vacina vacina = listaVacinas.get(i);
        myViewHolder.vacina.setText(vacina.getTipoVacina());
        myViewHolder.doses.setText(vacina.getDoses());

         //Muda cor do texto das vacinas com saldo 0 para vermelho
       if(vacina.getDoses() == "0 doses" ){
            myViewHolder.doses.setTextColor(context.getResources().getColor(R.color.colorAccent));
        }
    }

    @Override
    public int getItemCount() {
        return listaVacinas.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView imagem;
        TextView vacina;
        TextView doses;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            imagem = itemView.findViewById(R.id.imageSeringa);
            vacina = itemView.findViewById(R.id.textVacina);
            doses = itemView.findViewById(R.id.textDoses);

        }
    }

}

Thanks in advance for your willingness to help me...

  • setTipoVacina... setDoses... are faded, I still don’t understand why.

  • This happens because you do not use these methods. Already the log indicates that, probably, the properties names in the Firebase are different from the names of setters and getters

  • That’s the question Ivan, how are they not being used? These were automatically generated by Android Studio for such function.

  • You need to call them: vacina.getVacina() e vacina.getDose(). But that’s not a mistake.

  • It still doesn’t work here, the Recyclerview does not mirror my data I added to Firebase.

  • Edit the question by adding the code from adapter, of activity and of Firebase Database (do not forget to shred some sensitive information). Only with this question information will not be possible to solve your problem.

  • Written @Ivansilva, I hope it’s a little clearer.

Show 2 more comments

1 answer

0

So come on Edielson!

First let’s be clear: the fact that the methods are dimmed only indicates that they are not being used. It is not an error. Done, let’s get to the real problem.

The problem

Your problem is that when you create a in the Real Time Database it needs to be a kind of copy of your model (class). Thus, when updating or recovering this data, programmatically, with the SDK of Firebase, it will map all properties of the based on the names of the class fields you pass as parameter for the getValue() down in the onDataChange.

The important point is how the data will be stored in the database. This is something relative to the project and depends on how these data will be recovered in an easier way. But seeing that you only need a simple list of vaccines, we can modify your current structure for something like this

cs-i-vacinas-stock
      vacinas
         key_vacina_1
             nome
             tipo
             doses
         key_vacina_2
             nome
             tipo
             doses
             .
             .
             .
        key_vacina_N
            nome
            tipo
            doses

As keys must be defined in this structure. They will differentiate each of the list (árvore). And this can be done automatically by SDK when you save data by app, programmatically. Or it should be done manually if you insert the data directly into the console.

Solution

To solve your problem using the tips that have been given, we will need to modify your code. Then follow these steps to implement the solution.

1 - Modify your class Vacina.java removing the call code to Firebase. The only purpose of the class is to model your data and not make calls to database.

public class Vacina {
   private String nome;
   private String tipo;
   private int doses;

   public Vacina() {
       // Esse construtor vazio é requerido pelo Firebase
   }

   public String getNome() {
      return nome;
   }

   public void setNome(String nome) {
      this.nome = nome;
   }

   public String getTipo() {
      return tipo;
   }

   public void setTipo(String tipo) {
      this.tipo = tipo;
   }

   public int getDoses() {
      return doses;
   }

   public void setDoses(int doses) {
      this.doses = doses;
   }
}

2 - If the vaccines are saved programmatically, the Firebase will generate keys automatically just needing you to call push()

Vacina vacina = new Vacina();
       vacina.setName("Tríplice Viral");
       vacina.setTipo("B");
       vacina.setDoses(1);

vacinasRef.child("vacinas")
       .push()   // Isso gerará uma nova key
       .setValue(vacina);

3 - If you want to enter the data manually, always generate a key for each new vaccine included in the list (use this website in part Codeigniter Encryption Keys), more or less like this

cs-i-vacinas-stock
      vacinas
         dSYaFIUqY5oGw34gcwdiOHQIYmreyfws
                   nome: "Tríplice Viral"
                   tipo: "B"
                   doses: 1
         gbJw8JwhLhwMq7hBTHZn2fStsEhTFxfp
                   nome: "Tetraviral"
                   tipo: "A"
                   doses: 1

5 - Note that the name of the properties of are the same as the fields in your model (name, type and doses). Any other property that does not exist in your model will be alerted in the log that no corresponding field was found. But this can be easily solved by creating the field and its getters and setter on the model.

I think you should take another look at documenting of Firebase to get acquainted with the concept of the thing.

I hope I’ve been clear and this can help you. And anything just ask. Hug!

  • Hello @Ivansilva, I made the modifications you showed me, I removed the call to Firebase of my class Vaccine.java, modify the structure of the Realtime Database adding different keys to each product (vaccine) manually - I want to work with a maximum of 13 vaccines, so it’s easy to add them - and finally I checked that the names of the class properties are the same as my structure in Firebase. Upshot: The log notice consists, the sets remain dimmed and my app does not list the items (vaccinations). If you need to view the modifications, I can add them to the question.

  • Sorry for the delay, I’m working a lot lately. Pointing out again that the methods are faded does not set up an error. If the logs persist, you did not use the same structure that I proposed. If possible, make a second edit on the question (keeping current information) with your class and logs.

  • So @Ivansilva all right here partner, I want to leave here my thanks to the community and especially to you, through your answer I modified my structure JSON with the same attributes names of my class Java vaccine. and the thing began to flow. Really the methods Setters was not the problem, as proof of this, they continue to fade with the application working normally (laughs). The real problem with my app was that my class Configuracaofirebase.java was already capturing the instance of Firebase and I didn’t even realize it CONTINUES >>>

  • So after giving the neurons toast here, I was able to identify this problem. I took the call vacinasRef.child("VACINAS") of my main class and put the reference "VACCINES" the mirror that is in the structure of Firebase, as path of getReference in my class Configuracaofirebase.java. Stayed like this: firebase = FirebaseDatabase.getInstance().getReference("VACINAS"); done these small modifications the application decided to work listing instantly in my Recyclerview all vaccines and quantities of these which I add to Firebase. Show!

Browser other questions tagged

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