Anis Kaloun
Anis Kaloun

Reputation: 190

Android-Writing in Firebase Database an Arraylist inside of a node

I want to write an ArrayList ArticleDesire of Article.Class inside a User of my database, my Article class has a String titre and an ArrayList motcle, in the database the motcle array won't show up in my Database i Checked and it wasn't empty Dataset

Here is my Add ArticleDesire Method

 public void addArticleDesire(final ArrayList<Article> motcle, String Userid)
    {
        mDatabase = FirebaseDatabase.getInstance().getReference("User");
        DatabaseReference user = mDatabase.child(Userid);
        final DatabaseReference keylist = user.child("ArticleDesire");
        keylist.setValue(motcle);
   }

My Article constructor

 public Article(ArrayList<String> arrayList, String titre) {
        if (arrayList.isEmpty()){
            Log.w("Arraylist","VIDE");
        }
        this.motcle=new ArrayList(arrayList);
        if (motcle.isEmpty()==false)
        {
            Log.w("Arraylist"," NON VIDE"+motcle);
        }
        this.titre=titre;
    }

I call the AddArticleDesire in an activity, I Checked the constructor when called and the ArrayList isn't empty and the articles are in it.
is it possible when having different constructors in the Article Class, it can interfere with the Writing?

Upvotes: 1

Views: 418

Answers (1)

Francisco Durdin Garcia
Francisco Durdin Garcia

Reputation: 13327

Firebase Realtime Database doesn't store arrays. It stores sets of information(dictionaries/associate arrays). So the closest solution you can get is:

countries: {
  0: "Spain",
  1: "France",
  2: "Italy",
  3: "Poland",
  4: "Belgium"
}

So this should be the solution that you are looking for. If you wanna read more information and best practices regarding arrays in Realtime Database I suggest you to give a look to the next article: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

Finally, if you recently started to work on your app and your database, I suggest you to switch it to Firestore, which allows you to store arrays: https://firebase.google.com/docs/firestore/solutions/arrays?hl=es-419

Upvotes: 1

Related Questions