How to store and use a vector in Firebase on Android?

Asked

Viewed 467 times

1

I have a problem developing my app that I can’t fix and I can’t find anything that would help me. Such an app was running smoothly with data stored in variables, but when including Firebase I had some problems storing an array.

I made an algorithm in a class that receives and processes two vectors that are initially stored in variables:Horários armazenados em variáveis

The idea is to be able to store an array(array) in firebase and be able to use it normally in my app, and I can use its properties, such as obtaining values array(i), size array.length, etc... because I need to feed another class:

public class Busca_Horario {

    public String[] timeSelect(String[] entrada1, String[] entrada2){

}

Several online tutorials use obsolete firebase commands and did not serve me. Please help me out!

2 answers

1

The Firebase has no recourse to distinguish between a String and a Array, then you will have to create your own vectors.

I usually separate the values using a ;: -07:35;07:14;08:25;09:65;.....n

So when you go get these values, just give a split in String incoming:

 String[] valoresFirebase = getFirebaseValues().split(';');

Causing it to create its vector.

1


Based on Matheus' answer, it follows my functional result:

I added a simple string to a Child in Firebase and used the following command to find this string and turn it into an array of the type String[].

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String string = dataSnapshot.getValue().toString();
        String[] array = string.split(",");

        for (int i = 0; i < array.length; i++ )
        System.out.println(array[i]); //Para observar o seu resultado
    }

Thus, the method split() separates the array each time it finds the "," (comma, in this case, or any other term) in an array position String[] array.

  • Good Edeson, glad I could help.

Browser other questions tagged

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