1

I am creating an app for Tailoring shop owners. They can save measurements of customer. For this I have created pojo for each item,like Shirt class, Trouser class (pojo). But I want to add feature like if shop owner decides to add extra field like front, shoulder,etc. I am using Firebase RT DB to store data.

DatabaseReference rootRef;

rootRef = FirebaseDatabase.getInstance().getReference();

Shirtpojo shirt = new Shirtpojo (height, chest);

rootRef.child("shirt").setValue(shirt);

Here, parameters in Shirtpojo are predefined like,

class Shirtpojo{
String height,chest;
getHeight(){...}
setHeight{...}
}

But if tailor wants to add fields of his own he cant do it. Also to save the subsequent values of this new parameter I will need new layout(new edittext) in xml as well. How can I achieve this ?xml layout screenshot

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Adarsh Shete
  • 191
  • 1
  • 5
  • 1
    You can have a nested node in your database and call it "customFields" and you can retrieve this node as a Map object in your POJO – Quanta Jun 21 '20 at 14:53

1 Answers1

1

Unfortunately you can't tell Firebase to put the custom fields in a map or something like that.

The best I can think of is to create a child node for the custom properties.

"shirt": {
  "height": 173,
  "chest": 42,
  "customProperties": {
    "preferedName": "puf",
    "preferedColor": "yellow"
  }
}

And then a Java class:

public class Shirtpojo {
  public double height;
  public double chest;
  public Map<String, Object> customProperties;
}

Note: above syntax with public fields is the minimal POJO, but the nested map will also work if you use getters and setters.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for the answer, But then how can I create XML layout(i.e.EditText & TextView) for this ? – Adarsh Shete Jun 21 '20 at 17:28
  • 1
    You can't dynamically generate the view XML, but you *can* dynamically generate the views themselves. See https://stackoverflow.com/questions/45061731/solution-to-build-dynamic-forms-with-android or more from this: https://www.google.com/search?q=android+how+to+generate+dynamic+fields – Frank van Puffelen Jun 21 '20 at 19:53