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 console
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With