3

I am currently working on a android application which generates the input fields dynamically based on schema provided by the php server in json format. I know how to create fields dynamically in android.

I would like get some suggestions for json schema format, because there are number of input types say EditText, Spinner, RadioGroup, RadioButton, CheckBox, DatePicker, TimePicker, Camera Image, User Signature, etc. They may have default values, Spinners have multiple values, DatePicker and TimePicker have max and min restrictions, EditText input types may differ say number, decimal, text, multiline etc.,

Some suggestions on the json schema would be more helpful because am not much into json and it should also be easy to parse in android.

Sankar V
  • 4,794
  • 3
  • 38
  • 56

1 Answers1

2

This is an interesting project. I would suggest you to follow exact same hierarchy in your model classes as done by Android. Each model class will be specific to the view its made for. So for instance an EditTextModel would include fields like inputType whereas TextViewModel may include just text etc. Now all you need is google gson library and some custom adapter code for gson.

Detail: json could be like this.

{
"views": [
    {
        "class": "com.vj.TextViewModel",
        "properties": {
            "text": "hello world",
            "textColor": "#000000"
        }
    },
    {
        "class": "com.vj.EditTextModel",
        "properties": {
            "inputType": "number",
            "textSize": 20
        }
    }
]
}

with TextViewModel class:

public class TextViewModel implements Viewable{
    String text;
    String textColor;

    @Override
    public View getView(Context context){
        // generate and return view
        return view;
    }
}

and EditTextModel class:

public class EditTextModel implements Viewable{
    String inputType;
    int textSize;

    @Override
    public View getView(Context context){
        // generate and return view
        return view;
    }
}

where

public interface Viewable{
    public View getView(Context context);
}

As far as generic json serialization/deserialization and adapter code is concerned please watch and examine this code very carefully as it does exactly what you are asking for with similar models I generated above. https://stackoverflow.com/a/8683689/1112882

After your json is parsed and your ArrayList or Collection of Viewable is ready, just iterate over and call getView(context). Cheers... :)

Community
  • 1
  • 1
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • ya I found the answer helpful but I have a doubt. how can I read the values from various fields? My thought was creating a interface like Viewable with the method getValue() and implementing in all viewable classes and reset() method in the same interface for resetting the form fields. am I right? – Sankar V Oct 06 '13 at 05:17
  • You will use gson library. Gson is responsible for reading and writing values dynamically. – M-Wajeeh Oct 06 '13 at 09:00
  • You don't need to read values. In your getView method you will read class variables and apply on View and return it. But still if you want a generic interface for reading values from Model classes (although I don't know why it is necessary) then you need to use reflection. – M-Wajeeh Oct 06 '13 at 09:04
  • really I dont think that gson can read values from the model because in case of EditText it is getText(). for spinner it is getSelectedItem(), for CheckBox it is isChecked() and for RadioGroup, Signature and CameraImage its different. – Sankar V Oct 06 '13 at 10:15
  • `gson can read value from model` What ???? Gson will read json string and will create object of your model class and will assign values to variables by reading json. Have you ever used gson??? Stop assuming things and at least try to code a simple scenario. – M-Wajeeh Oct 06 '13 at 10:30
  • I am afraid that u misunderstand my comment. I am talking about reading the values from fields when the form is submitted. I have already used gson while working with Google places API but not completely aware of it. – Sankar V Oct 06 '13 at 12:35
  • Add a method `public String onSubmit()` in Viewable interface. This method will be responsible for generating json values for that particular View. These model classes will have all updated data inside them because in there `getView()` methods you would have assigned all listeners. So e.g. in `TextView` you would have assigned text change listener. Another way of doing this whole which is simple and better in many ways is this: in `getView()` method, before returning view just call `view.setTag(this);` and then when submit button is pressed just iterate through all Views and call this: – M-Wajeeh Oct 06 '13 at 12:44
  • `String jsonForThisView = ((Viewable)view.getTag).onSubmit(view);` this way every model class will get the view back in `onSubmit()` and will cast it to appropriate view object and will read values specifically for that view and will generate json for that view. – M-Wajeeh Oct 06 '13 at 12:46
  • `String jsonForThisView = ((Viewable)view.getTag()).onSubmit(view);` – M-Wajeeh Oct 06 '13 at 12:53