我将在下面附上我的代码,但要点是这样的:我正在将自定义类写入我的 firebase 控制台,并且一切正常,但我不断将一个额外的变量保存到数据库中,但仍然很奇怪它被命名为“true”并设置为“false”的事实。我不知道这是怎么发生的,也不知道为什么会发生。有什么想法吗?
图片
问题.java
public class Question {
String details,title;
Boolean isTrue;
String source;
double explanation;
Question(String t, String q, Boolean d, String s, double r) {
details = q;
title = t;
isTrue = d;
source = s;
explanation = r;
}
Question() {
details = "Blank";
title = "Blank";
isTrue = true;
source = "http://google.com";
explanation = 0.0;
}
Question(String rawData) {
int count = 0;
for (int i = 0; i < rawData.length(); i++)
{
if (rawData.charAt(i) == 'ß')
{
count++;
}
}
if (count != 5)
{
details = "Blank";
title = "Blank";
isTrue = true;
source = "http://google.com";
explanation = 0.0;
} else {
String[] values = rawData.split("ß");
title = values[0];
details = values[1];
isTrue = false;
if (values[2].equals("T"))
isTrue = true;
source = values[3];
explanation = Double.parseDouble(values[4]);
}
}
String getDetails() { return details; }
String getTitle() { return title; }
Boolean isTrue() { return isTrue; }
String getSource() { return source; }
double getExplanation() { return explanation; }
public void setDetails(String q) { details = q; }
public void setTitle(String t) { title = t; }
public void setTrue(Boolean i) { isTrue = i; }
public void setSource(String s) { source = s; }
public void setExplanation(Double r) { explanation = r; }
public String toString() {
String s = title + "ß" + details + "ß";
if (isTrue)
s += "T";
else
s += "F";
s += "ß";
s += source + "ß" + Double.toString(explanation);
return s;
}
}
写入数据库
title = (EditText)findViewById(R.id.add_title);
details = (EditText)findViewById(R.id.add_details);
source = (EditText)findViewById(R.id.add_source);
truth = (CheckBox)findViewById(R.id.add_truth);
explanation = (EditText)findViewById(R.id.add_explanation);
if (explanation.getText().equals(""))
explanation.setText("0.0");
submit = (Button) findViewById(R.id.add_submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Question questionToAdd = new Question(title.getText().toString(),details.getText().toString(),truth.isPressed(),source.getText().toString(),Double.parseDouble(explanation.getText().toString()));
Toast.makeText(AddQuestion.this,mDatabase.child("Quotes").toString(),Toast.LENGTH_SHORT).show();
try {
mDatabase.child("Quotes").push().setValue(questionToAdd).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(AddQuestion.this,"Success",Toast.LENGTH_SHORT).show();
}
});
} catch (RuntimeException e)
{
System.out.println(e.toString());
}
}
});
最佳答案
我正在使用简单的“toHashMap”函数,例如:
public HashMap toHashMap() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("details", details);
map.put("title", title);
map.put("isTrue", isTrue);
map.put("source", source);
map.put("explanation", explanation);
return map;
}
然后:
....
mDatabase.child("Quotes").push().setValue(questionToAdd.toHashMap())....
关于android - 将自定义对象添加到 firebase 会产生名为 "true"的无关数据成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40119347/