Android DataSnapShot对象';s值返回0而不是实际值
我正在做一个在线教程中的实时跟踪应用程序,这里我使用firebase设置状态系统。但它返回的是0而不是实际值: 来自我的firebase的图像: 这是我的问题课:Android DataSnapShot对象';s值返回0而不是实际值,android,firebase-realtime-database,Android,Firebase Realtime Database,我正在做一个在线教程中的实时跟踪应用程序,这里我使用firebase设置状态系统。但它返回的是0而不是实际值: 来自我的firebase的图像: 这是我的问题课: String question ; int level , operator , answer; public question() { } public question(int answer, int level, int operator , String question) { this.question
String question ;
int level , operator , answer;
public question() {
}
public question(int answer, int level, int operator , String question) {
this.question = question;
this.answer = answer;
this.level = level;
this.operator = operator;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getOperator() {
return operator;
}
public void setOperator(int operat) {
this.operator = operator;
}
@Override
public String toString() {
return question + " =" ;
}
主要活动:
List<question> questions ;
FirebaseDatabase database;
DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opertion);
questions = new ArrayList<>();
questions = readFromFireBase();
setUpClick();
}
private void setUpClick() {
btnbuttonThatSet.setOnClickListener(this);
btnbuttonOk.setOnClickListener(this);
}
@Override
public void onClick(View view) {
case R.id.buttonOk:
theRealAnswer =theAnswer.getText().toString();
correctAnswer = questions.get(questionNumber).answer;
inputQuestionOperator = questions.get(questionNumber).operator;
//this is for check
Log.d(" the question",questions.get(questionNumber).question+
" answer : "+questions.get(questionNumber).answer +
" operator : "+questions.get(questionNumber).operator +
" level : "+questions.get(questionNumber).level);
}
}
public ArrayList<question> readFromFireBase(){
ArrayList<question>arrayList = new ArrayList<>() ;
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Questions");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Log.d("", "Value is: " + postSnapshot.getValue(question.class).operator);
questions.add(postSnapshot.getValue(question.class));
}
questionNumber = getRandomElement(questions);
textView2.setText(questions.get(questionNumber).question);
Log.d("", "Value is: " + questions.get(questionNumber).operator);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
return arrayList;
}
// Function select an element base on index
// and return an element
public int getRandomElement(List<question> list)
{
Random rand = new Random();
return rand.nextInt(list.size());
}
列出问题;
Firebase数据库;
数据库参考myRef;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u Operation);
问题=新的ArrayList();
questions=readFromFireBase();
设置单击();
}
私有void setUpClick(){
btnbuttonatset.setOnClickListener(此);
btnbuttonOk.setOnClickListener(这个);
}
@凌驾
公共void onClick(视图){
外壳R.id.按钮编号:
theRealAnswer=theAnswer.getText().toString();
correctAnswer=问题。获取(问题编号)。回答;
inputQuestionOperator=questions.get(questionNumber).operator;
//这是支票
Log.d(“问题”,questions.get(questionNumber).question+
回答:“+问题。获取(问题编号)。回答+
接线员:“+问题.获取(问题编号).接线员+
级别:“+问题.get(问题编号).level);
}
}
公共ArrayList readFromFireBase(){
ArrayList=新的ArrayList();
database=FirebaseDatabase.getInstance();
myRef=database.getReference(“问题”);
myRef.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//使用初始值调用此方法一次,然后再次调用
//每当更新此位置的数据时。
对于(DataSnapshot postSnapshot:DataSnapshot.getChildren()){
Log.d(“,”值为:“+postSnapshot.getValue(question.class).operator);
添加(postsnashot.getValue(question.class));
}
问题编号=getRandomElement(问题);
textView2.setText(questions.get(questionNumber.question));
Log.d(“,”值为:“+questions.get(questionNumber.operator)”;
}
@凌驾
已取消的公共无效(DatabaseError错误){
//无法读取值
//Log.w(标记“读取值失败”,error.toException());
}
});
返回数组列表;
}
//函数根据索引选择元素
//并返回一个元素
公共int getRandomElement(列表)
{
Random rand=新的Random();
返回rand.nextInt(list.size());
}
这是使用firebase运行时运行代码后的结果:
一定是同一件事。。所有数据都没有任何问题(问题、答案、级别),但运算符始终为0,这与firebase中的数据不符您在
设置运算符方法
上设置了错误的值。我猜你打错了
setOperator(int operat){
this.operator = operator;
}
而不是
setOperator(int operator){
this.operator = operator;
}