Error with firebase and Android studio Failed to convert value of type java.lang.Long to String

0

I have a mistake that I would like to see if you can help me, what happens is that in firebase I enter a long string manually which I want to be in my application

in my code I have the following to bring the data from the base, the scrolltext .Stext is where I have to put the string because if I put it manually if I get it

 db = FirebaseDatabase.getInstance();
            auth = FirebaseAuth.getInstance();
            user = auth.getCurrentUser();
            usersRef = db.getReference("Cuentos" ).child("Cuento1");
            usersRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot objetsnatshot : dataSnapshot.getChildren()){

                        Cuentos s1=dataSnapshot.getValue(Cuentos.class);
                        String stringcuento=s1.getHistory();


                        ScrollTextView scrolltext=(ScrollTextView) findViewById(R.id.scrolltext);
                        scrolltext.setText(stringcuento);
                        scrolltext.startScroll();



                    }

I also put my class to be story stories

public class Cuentos {
public String history,Nivel,Nombre;

public Cuentos(String historystring, String nivelcuento, String nombrecuento ) {
    this.history = historystring;
    this.Nivel = nivelcuento;
    this.Nombre = nombrecuento;


}

public Cuentos() {}

public String getHistory() {
    return history;
}

public void setHistory(String history) {
    this.history = history;
}

}

and the error message is as follows

ALREADY TRY TO CHANGE LONG AND VISEVERSA I do not know if you can help me solve this and if you can put a long so long in firebase

    
asked by Julio Tobar 23.11.2018 в 10:24
source

1 answer

1

The problem is clear: Failed to convert value of type java.lang.Long to String means that you try to convert a value of type Long to String .

When mapping the data to class Cuento , it is found that the value Nivel is recognized in Firebase as type Long (because it is not written in quotation marks), but in the class you have it declared as String :

public String history,Nivel,Nombre;

Solutions

One (not recommended if Nivel will always be numeric)

If the value should be treated as String, you must write it in Firebase surrounded by quotes: "1" . Paying strict attention that this is in all the nodes , because in any node that you write the data without quotes it will give you the error, because in the class it is declared as String.

Two (recommended if Nivel will always be numeric)

If the value should be treated as Long , or as int , you have to declare it as such in the class, and change that parameter in the constructor, also in the getter and the setter if necessary:

public class Cuentos {
    public String history,Nombre;
    /*Aquí es declarado como Long*/
    public Long Nivel;

    /*También en el constructor*/
    public Cuentos(String historystring, Long nivelcuento, String nombrecuento ) {
        this.history = historystring;
        this.Nivel = nivelcuento;
        this.Nombre = nombrecuento;
    }

    public Cuentos() {}

    public String getHistory() {
        return history;
    }

    public void setHistory(String history) {
        this.history = history;
    }

    /*También en el getter*/
    public Long getNivel() {
        return Nivel;
    }

    /*También en el setter*/
    public setNivel (Long Nivel) {
        this.Nivel=Nivel;
    }
}
  

NOTE: I recommend that you respect the Java naming convention . I've put your variables like you had, but they're the same   incoherent in terms of the naming convention. This is not a reason   error, but the code is less clear and will give you a problem if some   day some other programmer goes to work together in your   application.

    
answered by 23.11.2018 в 11:16