package com.github.dkharrat.nexusdialog.sample; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.dkharrat.nexusdialog.FormController; import com.github.dkharrat.nexusdialog.FormFragment; import com.github.dkharrat.nexusdialog.FormInitializer; import com.github.dkharrat.nexusdialog.FormManager; import com.github.dkharrat.nexusdialog.FormModel; import com.github.dkharrat.nexusdialog.controllers.CheckBoxController; import com.github.dkharrat.nexusdialog.controllers.EditTextController; import com.github.dkharrat.nexusdialog.controllers.FormSectionController; import com.github.dkharrat.nexusdialog.controllers.SelectionController; import com.github.dkharrat.nexusdialog.utils.MessageUtil; import java.util.Arrays; import java.util.HashSet; /** * FormFragment provides a default Fragment implementation for using NexusDialog. */ public class AlumFragment extends FormFragment { private FormManager formManager; public static final String FIRST_NAME = "nombre"; public static final String LAST_NAME = "apellido"; public static final String GENDER = "gender"; public static final String HOBBIES = "hobbies"; @Override public void initForm(FormController controller) { Context ctxt = getContext(); FormSectionController section = new FormSectionController(ctxt, "Personal Info"); section.addElement(new EditTextController(ctxt, FIRST_NAME, "First name")); section.addElement(new EditTextController(ctxt, LAST_NAME, "Last name")); section.addElement(new SelectionController(ctxt, GENDER, "Gender", true, "Select", Arrays.asList("Male", "Female"), true)); section.addElement(new CheckBoxController(ctxt, HOBBIES, "You like", true, Arrays.asList("sport", "gaming", "relaxation", "development"), true)); HashSet checkedBoxes = new HashSet(); checkedBoxes.add("gaming"); controller.getModel().setValue("hobbies", checkedBoxes); controller.addSection(section); } public static final AlumFragment newInstance() { AlumFragment f = new AlumFragment(); return f; } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.alum_activity, null); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); formManager = new FormManager(this, this, R.id.form_elements_container); recreateViews(); } public FormController getFormController() { return formManager.getFormController(); } public FormModel getModel() { return getFormController().getModel(); } protected void recreateViews() { formManager.recreateViews(); } public boolean validate() { getFormController().resetValidationErrors(); if (getFormController().isValidInput()) { Object firstName = getModel().getValue(FIRST_NAME); Object lastName = getModel().getValue(LAST_NAME); Object gender = getModel().getValue(GENDER); Object favColor = getModel().getValue(HOBBIES); String msg = "First name: " + firstName + "\n" + "Last name: " + lastName + "\n" + "Gender: " + gender + "\n" + "Hobbies: " + favColor + "\n"; MessageUtil.showAlertMessage("Field Values", msg, getActivity()); } else { getFormController().showValidationErrors(); } return true; } }