Search code examples
javaandroidarraylistfirebase-realtime-databasegetter-setter

How should I set up getter and setter which is key - value type in Firebase Realtime Database?


I have this Firebase Realtime Database

my Firebase Realtime Database

And when I want to display it goes with error :

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.ArrayList to String

Displaying works fine without "players" child in database. Error is in MyEvents.java class in this line event = ds.getValue(Event.class);. Here is my Event.java class with getters, setters and constructors

package com.example.vilniausfutbolas;

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class Event {

    public String team_name;
    public String date;
    public String time;
    public String location;
    public Integer quantity;
    public Double price;
    public String additional;
    public String players;
    public String refused_players;
    public String event_id;

    public Event() {
        // Default constructor required for calls to DataSnapshot.getValue(Event.class)
    }

    public Event(String userId, String team_name, String date, String time, String location, Integer quantity, Double price, String additional, String players, String refused_players, String event_id) {

        this.team_name = team_name;
        this.date = date;
        this.time = time;
        this.location = location;
        this.quantity = quantity;
        this.price = price;
        this.additional = additional;
        this.players = players;
        this.refused_players = refused_players;
        this.event_id = event_id;
    }


    public String getTeam_name() {
        return team_name;
    }

    public void setTeam_name(String team_name) {
        this.team_name = team_name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getAdditional() {
        return additional;
    }

    public void setAdditional(String additional) {
        this.additional = additional;
    }

    public String getPlayers() {
        return players;
    }

    public void setPlayers(String players) {
        this.players = players;
    }

    public String getRefused_players() {
        return refused_players;
    }

    public void setRefused_players(String refused_players) {
        this.refused_players = refused_players;    }

    public String getEvent_id() {
        return event_id;
    }

    public void setEvent_id(String event_id) {
        this.event_id = event_id;
    }
}

So I guess the problem is to set players getter and setter correctly because of the error. Or maybe there is other way how to deal with players child that I could display event in one object with one constructor from Event.java class?

Maybe I should create separate class for players child?


Solution

  • In your database, players field is a list, not a String. So, you should declare players as a list in your Event.java model class like this: public List<String> players;
    Then replace the same in your constructor. Then replace the getter and setter with this code:

    public List<String> getPlayers() {
       return players;
    }
    
    public void setPlayers(List<String> players) {
        this.players = players;
    }
    

    Or, better let Android Studio auto generate constructor, getter and setter.