提交带有动态创建的表单输入的 json 请求

Posted

技术标签:

【中文标题】提交带有动态创建的表单输入的 json 请求【英文标题】:Submit json request with inputs of a form created dynamically 【发布时间】:2019-09-16 22:14:23 【问题描述】:

我正在使用 API 请求接收的输入创建表单,我使用 switch 来创建每个表单,它的 'type' ,例如 if type=='text' var = editText , type=='title ' var = 'textview' 并且如果 type='submit' var = button。

我在 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",
    ...
  
]

【问题讨论】:

哪些变量?为什么不全局定义它们,然后在 switch case 中覆盖它们,最后你构建你的 JSON 数组......很难得到你想要的。 你能举个例子吗? 你甚至不说是哪些变量。 【参考方案1】:
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。 真的很难得到你想要的。

【讨论】:

不,这不是我需要的 抱歉,我是经验丰富的 android 开发人员,但我没有得到您想要的。花一些时间来说明你的问题。我相信你只想要一个可以通过 POST 发送的 Json。哥们,谷歌一下。例子太多了。

以上是关于提交带有动态创建的表单输入的 json 请求的主要内容,如果未能解决你的问题,请参考以下文章

使用 JSON 格式的 POST 方法 web api 将动态输入提交到数据库

带有动态创建行的表的表单

从 json 动态创建的 Javascript 表单输入,需要将对象映射到另一个对象的正确字段并保存

如何创建动态 html 表单的 JSON 对象(表单元素)?

为啥这种带有动态输入值的表单验证不起作用?

使用动态输入将表单数据序列化为 JSON