12

I am new to FIrebase and I have 2 problems with it that might be connected. First one is when saving my list of events.

//creating event
TVEvent tvEvent = new TVEvent(etTitle.getText().toString());
User host = ResourceManager.getUser();
String date = etDate.getText().toString();
String location = etLocation.getText().toString();
TVSet tvset = ResourceManager.getUser().getTvSets().get(0);
Event ev = new Event(tvEvent, host, date, location, tvset);
ResourceManager.addEvent(ev);
mDatabase.child("events").child(host.getId()).setValue(ResourceManager.getEvents()); //getEvents() returns a list of events

this is what I get in the consoleenter image description here

The problem is that tvevent and tv set have more attributes than these ones. When I debugged to find out why is that tvevent was created with all of its attributes which was a little strange. For now, however I don't know whether this is a problem as I cannot retrieve the tvset and tvevent from the database. When i do the following I get the to be null.

mDatabase.child("events").addListenerForSingleValueEvent(
    new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println("Cheking for events");
            //GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {};
            //List<Event> e = dataSnapshot.getValue(t);
            for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
                GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {};
                List<Event> list = messageSnapshot.getValue(t);
                if (list != null) {
                    for (Event ev : list) {
                        System.out.println("Event found");
                        ResourceManager.addEvent(ev);
                    }
                }
            }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I don't know why these values are null as I can see that they are not in the firebase console. So what is the problem?

@IgnoreExtraProperties
public class Event {

private TVEvent tvEvent;
private User host;
private Date date;
private String location;
private TVSet tvSet;
private List<User> attending;

public Event(){
}

public Event(TVEvent tvEvent, User host, String date, String location, TVSet tvset){
    this.tvSet = tvset;
    this.tvEvent = tvEvent;
    this.host = host;
    try {
        this.date = new SimpleDateFormat("dd/MM/yyyy").parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    this.location = location;

    attending = new ArrayList<>();
}

public User getHost() {
    return host;
}

public String getLocation() {
    return location;
}

public TVSet getTVSet() {
    return tvSet;
}

public Date getDate() {
    return date;
}

public TVEvent getTVEvent() {
    return tvEvent;
}

public void addAttending(User user){
    attending.add(user);
}

public List<User> getAttending(){
    return attending;
}
}

As both TVSet and TVEvent does not get deserialised properly I will post only one of them:

@IgnoreExtraProperties
public class TVSet {

private String title;
private String imageFile;
//private Bitmap picture;

public TVSet(){

}

public TVSet(String title){
    this.title = title;
}

public TVSet(Bitmap picture){
    imageFile = compressImage(picture);
}

public void setTitle(String title){
    this.title = title;
}

public String getTitle() {
    return title;
}

private String compressImage(Bitmap image){
    ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
    //image.recycle();
    byte[] byteArray = bYtE.toByteArray();
    String imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
    return imageFile;
}


@Exclude
public Bitmap getImage() {
    byte[] decodedString = Base64.decode(imageFile, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    return bitmap;
}

public String getImageFile(){
    return imageFile;
}
}

Sorry for the bad formatting. Any help is appreciated.

adolfosrs
  • 9,286
  • 5
  • 39
  • 67
Nikola
  • 890
  • 1
  • 11
  • 35
  • Your structure is like that as i understood: userid->list_item_index->Event. So you should try GenericTypeIndicator> instead of GenericTypeIndicator>. – ugur Jun 26 '16 at 08:39
  • I am able to run the code you posted and get better results. I'm using Firebase 9.0.2. What version are you using? – Bob Snyder Jun 29 '16 at 22:57
  • You say _When i do the following I get the to be null_. What is null: `list`, `dataSnapshot`? – Bob Snyder Jun 30 '16 at 00:31
  • List is null and I am using the same version of firebase. I am now trying a different approach - to store the images of the objects in the firebase-storage and not compress them as a String. I think that will work but I need some time to test it. – Nikola Jun 30 '16 at 15:48
  • http://stackoverflow.com/questions/38128241/firebase-google-causes-java-lang-stackoverflowerror this is another issue I have. – Nikola Jun 30 '16 at 16:05

1 Answers1

3

try to use

Map<String, String> data= (HashMap<String, String>)dataSnapshot.getValue();

instead of

GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {}; List<Event> list = messageSnapshot.getValue(t);

Sahaj Rana
  • 1,993
  • 4
  • 25
  • 42