JFed-9 JFed-9 - 1 month ago 8
Android Question

Adding custom object to firebase yields extraneous data member called "true"?

I'll attach my code below, but the gist is this: I am writing a custom class into my firebase console, and everything is working great, but I keep getting an extra variable saved into the database, and weirder still is the fact that it is entitled "true" and set to "false". I have no idea how or why this is happening. Any ideas?

Picture

Database

Question.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;
}
}


Writing to the database

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());
}
}
});

Answer

I'm using simple 'toHashMap' function, for example:

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;
}

And then:

.... 
mDatabase.child("Quotes").push().setValue(questionToAdd.toHashMap())....
Comments