我是FIrebase的新手,我有2个可能连接的问题。第一个是保存我的事件列表。
//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
中获得的内容
问题是tvevent和tv set具有比这些更多的属性。当我调试以找出为什么tvevent创建时具有所有属性,这有点奇怪。但是现在,我不知道这是否是一个问题,因为我无法从数据库中检索到tvset和tvevent。当我执行以下操作时,我将其设为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) {
}
});
我不知道为什么这些值为null,因为我可以看到它们不在firebase控制台中。那么问题是什么?
@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;
}
}
由于TVSet和TVEvent都没有正确反序列化,我只发布其中一个:
@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;
}
}
抱歉格式不正确。任何帮助表示赞赏。
答案 0 :(得分:3)
尝试使用
Map<String, String> data= (HashMap<String, String>)dataSnapshot.getValue();
而不是
GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {};
List<Event> list = messageSnapshot.getValue(t);