-
Notifications
You must be signed in to change notification settings - Fork 30
Start and obtain form data
Neat Form parses the JSON
form provided from the Android Assets
directory into a model that it uses to render the views. The supported views are registered first before the form is built. Once the views have been created as children to the the RootView
they are then passed to the FormBuilder
.
You can override the supported views with your own implementation(s) if the features provided out of the box by neat form do not suffice. However one thing to note is that you can only extend the classes that implement ViewBuilder
but not the NFormView
classes.
Code snippet for starting up a form
public void startForm(int jsonFormActivityRequestCode, String formName, String entityId, boolean translate) throws Exception {
String currentLocationId = "Kenya";
JSONObject jsonForm = getFormJson(formName);
if (jsonForm != null) {
jsonForm.getJSONObject("metadata").put("encounter_location", currentLocationId);
switch (formName) {
case "rules_engine_demo": {
Intent intent = new Intent(this, JsonWizardFormActivity.class);
intent.putExtra("json", jsonForm.toString());
Log.d(getClass().getName(), "form is " + jsonForm.toString());
Form form = new Form();
form.setName("Rules engine demo");
form.setWizard(true);
form.setNextLabel(getString(R.string.next));
form.setPreviousLabel(getString(R.string.previous));
form.setSaveLabel(getString(R.string.save));
form.setActionBarBackground(R.color.customAppThemeBlue);
form.setNavigationBackground(R.color.button_navy_blue);
form.setHideSaveLabel(true);
intent.putExtra(JsonFormConstants.JSON_FORM_KEY.FORM, form);
intent.putExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, translate); // Used to trigger MLS 2.0 on the forms
startActivityForResult(intent, jsonFormActivityRequestCode);
break;
}
default: {
break;
}
}
}
}
Code snippet to obtain data from the form.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String jsonString = data.getStringExtra("json");
Log.i(getClass().getName(), "Result json String !!!! " + jsonString);
}
super.onActivityResult(requestCode, resultCode, data);
}
After you have he JSON string then you can manipulate it to either save the data from the form or use the data however you feel fit.
NOTE: You cannot retrieve data from the form when the form is invalid (when any field is invalid or when a required field is missing); an error dialog will be displayed instead.
Forked from Android Native JSON Form Library. Adapted in love by the OpenSRP Community . Apache License, Version 2.0
Introduction
Core Features
Form
Views