【发布时间】:2017-06-12 10:23:51
【问题描述】:
当我尝试使用包含许多变量的类在 firebase 数据库上写入时,它可以正常工作,但是当我尝试检索数据时,应用程序崩溃了!! 为什么我会疯-_-!?
>java代码
FirebaseDatabase database;
DatabaseReference myRef;
TextView text1, text2, text3;
EditText et1, et2, et3;
Button bSave, bRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = FirebaseDatabase.getInstance();
myRef = database.getReference();
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
bSave = (Button) findViewById(R.id.bSave);
bSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str1 = et1.getText().toString();
String str2 = et2.getText().toString();
String str3 = et3.getText().toString();
myRef.child("Posts").push().setValue(new text(str1,str2,str3));
}
});
bRead = (Button) findViewById(R.id.bRead);
bRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myRef=database.getReference().child("Posts");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//doesn't work
text t1 = dataSnapshot.getValue(text.class);
//doesn't work
/*for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
text t1 = snapshot.getValue(text.class);
}*/
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("MainActivity", "loadPost:onCancelled", databaseError.toException());
}
});
}
});
}
}
> Text.class
public class text {
String text1,text2,text3;
public text(String text1, String text2, String text3) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
public String getText3() {
return text3;
}
public void setText3(String text3) {
this.text3 = text3;
}
}
>屏幕
【问题讨论】:
-
根据 Firebase 的需要添加空文本构造函数。公共文本(){}
-
uguboz .. 我爱你,男人
-
我作为答案发布,以便其他人可以看到。
标签: android firebase firebase-realtime-database