使用动态创建的表单输入提交JSON请求

时间:2019-04-27 18:55:14

标签: android json rest android-volley

我正在使用API​​请求接收到的输入来创建表单,我正在使用switch来创建具有“类型”的每个表单,例如,如果type =='text'var = editText,type =='title 'var ='textview',如果type ='submit'var =按钮。

我在API上使用了volley,我需要在用户提交表单后提交表单,但是我不知道如何获取变量及其数据列表(因为它们是动态创建的,我不知道)。

switch (type) {
                            case "text":
                                LinearLayout lyt = new LinearLayout(context);
                                lyt.setOrientation(LinearLayout.HORIZONTAL);
                                ViewGroup.LayoutParams prms = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
                                lyt.setLayoutParams(prms);

                                TextView textViewLabel = new TextView(context);
                                textViewLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                textViewLabel.setTypeface(null, Typeface.BOLD);
                                textViewLabel.setText(rowObj.getString("label"));
                                textViewLabel.setMaxWidth(150);
                                lyt.addView(textViewLabel);

                                EditText editText = new EditText(context);
                                LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                paramsL.setMargins(15, 0,0,0);
                                if(rowObj.has("maxLength")) {
                                    InputFilter[] fArray = new InputFilter[1];
                                    fArray[0] = new InputFilter.LengthFilter(Integer.parseInt(rowObj.getString("maxLength")));
                                    editText.setFilters(fArray);
                                }
                                editText.setLayoutParams(paramsL);
                                editText.setSingleLine(true);
                                editText.setWidth(500);
                                editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                lyt.addView(editText);

                                parent.addView(lyt);
                                break;
                            case "textarea":
                                LinearLayout lyt2 = new LinearLayout(context);
                                lyt2.setOrientation(LinearLayout.HORIZONTAL);
                                ViewGroup.LayoutParams prms2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
                                lyt2.setLayoutParams(prms2);

                                TextView textViewLabel2 = new TextView(context);
                                textViewLabel2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                textViewLabel2.setTypeface(null, Typeface.BOLD);
                                textViewLabel2.setMaxWidth(150);
                                textViewLabel2.setText(rowObj.getString("label"));
                                lyt2.addView(textViewLabel2);

                                EditText editText2 = new EditText(context);
                                LinearLayout.LayoutParams paramsL1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                paramsL1.setMargins(15, 0,0,0);
                                if(rowObj.has("maxLength")) {
                                    InputFilter[] fArray1 = new InputFilter[1];
                                    fArray1[0] = new InputFilter.LengthFilter(Integer.parseInt(rowObj.getString("maxLength")));
                                    editText2.setFilters(fArray1);
                                }

                                editText2.setLayoutParams(paramsL1);
                                editText2.setSingleLine(true);
                                editText2.setWidth(500);
                                editText2.setHeight(300);
                                editText2.setEms(10);
                                editText2.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5.0f,  getResources().getDisplayMetrics()), 1.0f);
                                editText2.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_CLASS_TEXT);
                                editText2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                lyt2.addView(editText2);

                                parent.addView(lyt2);
                                break;
                            case "image":
                                LinearLayout lLayout1 = new LinearLayout(context);
                                ViewGroup.LayoutParams params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
                                lLayout1.setLayoutParams(params1);

                                ImageView imageView = new ImageView(context);
                                imageView.setPadding(5,5,5,5);
                                Picasso.with(context).load(rowObj.getString("src")).into(imageView);
                                lLayout1.addView(imageView);

                                parent.addView(lLayout1);
                                break;
                            case "title":
                                TextView textView = new TextView(context);
                                ViewGroup.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, Float.parseFloat(rowObj.getString("colSpan")));
                                textView.setLayoutParams(params2);
                                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                textView.setTypeface(null, Typeface.BOLD);
                                textView.setText(rowObj.getString("label"));

                                parent.addView(textView);
                                break;
                            case "subtitle":
                                TextView textView1 = new TextView(context);
                                ViewGroup.LayoutParams params3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
                                textView1.setLayoutParams(params3);
                                textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                textView1.setText(rowObj.getString("label"));

                                parent.addView(textView1);
                                break;
                            case "file":

                                break;

                            case "radio":
                                LinearLayout lyt3 = new LinearLayout(context);
                                lyt3.setOrientation(LinearLayout.VERTICAL);
                                ViewGroup.LayoutParams prms3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
                                lyt3.setLayoutParams(prms3);

                                TextView textViewLabel3 = new TextView(context);
                                textViewLabel3.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                textViewLabel3.setTypeface(null, Typeface.BOLD);
                                textViewLabel3.setText(rowObj.getString("label"));
                                lyt3.addView(textViewLabel3);

                                // Initialize a new RadioGroup
                                RadioGroup rg = new RadioGroup(context);
                                rg.setOrientation(RadioGroup.VERTICAL);

                                if(rowObj.has("options")){
                                    JSONArray options = rowObj.getJSONArray("options");
                                    if(options.length()>0){
                                        for (int i=0; i<options.length(); i++){
                                            JSONObject radioObj = options.getJSONObject(i);
                                            // Create another Radio Button for RadioGroup
                                            RadioButton rb = new RadioButton(context);
                                            rb.setText(radioObj.getString("label"));
                                            rb.setTextColor(Color.BLACK);
                                            rg.addView(rb);
                                        }
                                    }
                                }
                                lyt3.addView(rg);

                                parent.addView(lyt3);
                                break;
                            case "submit":
                                Button button = new Button(context);
                                ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, Float.parseFloat(rowObj.getString("colSpan")));
                                button.setLayoutParams(params);
                                button.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                button.setSingleLine(true);
                                button.setText(rowObj.getString("label"));
                                button.setTextColor(Color.WHITE);

                                parent.addView(button);
                                break;
                            case "hidden":

                                break;
                        }
                    }else {
                        TextView view = new TextView(context);
                        ViewGroup.LayoutParams params3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
                        view.setLayoutParams(params3);
                        view.setBackgroundColor(Color.WHITE);
                        view.setText("");

                        parent.addView(view);
                    }
                }

我想将变量放入JSON数组中以发送POST请求:

[
  {
    "variable1": "value1",
    "variable2": "value2",
    ...
  }
]

1 个答案:

答案 0 :(得分:0)

public class Test extends Activity{


    private String variableOne;
    private String variableTwo;


     switch (whatever) {
        case "text":

              varbiableOne = "firstvaluetext";
              variableTwo="secondvaluetext" 

          break;

        case "textarea":

             varbiableOne = "firstvaluetextarea";
             variableTwo="secondvaluetextarea"  

          break;

        default:
        //whatever

          break;

        }

那里有您的变量,您可以从中构建json。 真的很难得到想要的东西。