【发布时间】:2016-06-22 21:59:13
【问题描述】:
我是 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。当我执行以下操作时,我得到为空。
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;
}
}
抱歉,格式错误。任何帮助表示赞赏。
【问题讨论】:
-
你的结构就像我理解的那样:userid->list_item_index->Event.所以你应该尝试 GenericTypeIndicator
-
我能够运行您发布的代码并获得更好的结果。我正在使用 Firebase 9.0.2。你用的是什么版本?
-
你说 当我执行以下操作时,我得到了 null。什么是空:
list、dataSnapshot? -
List 为空,我使用的是相同版本的 firebase。我现在正在尝试一种不同的方法 - 将对象的图像存储在 firebase-storage 中,而不是将它们压缩为字符串。我认为这会奏效,但我需要一些时间来测试它。
-
stackoverflow.com/questions/38128241/… 这是我遇到的另一个问题。
标签: android firebase firebase-realtime-database