1

我正在尝试U.I.JSON响应中创建动态。我有以下代码。

class LoadAllQuestions extends AsyncTask<String, String, String> {

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();
    JSONArray questions = null;

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading questions. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) {

        // getting JSON string from URL
        companyName = cn.getText().toString();
        projectName = pn.getText().toString();
        String componentName = (String) ab.getSelectedTab().getText();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("company", companyName));
        nameValuePairs.add(new BasicNameValuePair("project", projectName));
        nameValuePairs.add(new BasicNameValuePair("component",
                componentName));

        JSONObject json = jParser.makeHttpRequest(url, "POST",
                nameValuePairs);
        // Check your log cat for JSON response
        Log.d("All Questions: ", json.toString());
        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                Log.v("RESPONSE", "Success!");
                // products found: getting Array of Questions
                questions = json.getJSONArray(TAG_QUESTIONS);

                // looping through All Questions
                for (int i = 0; i < questions.length(); i++) {

                    JSONObject c = questions.getJSONObject(i);

                    // Storing each JSON item in variable
                    String name = c.getString(TAG_NAME);
                    String field = c.getString(TAG_FIELD);
                    String value = c.getString(TAG_VALUE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_NAME, name);
                    map.put(TAG_FIELD, field);
                    map.put(TAG_VALUE, value);

                    infoList.add(map);
                }

            } else {
                // no products found
                Log.v("ERROR", "No JSON for you!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String string) {
        // dismiss the dialog
        pDialog.dismiss();
        // loop through infoList
        for (int i = 0; i < infoList.size(); i++) {
            // get HashMap
            HashMap<String, String> map = infoList.get(i);              
            // if the answer should be a radio button, inflate it
            if (map.get(TAG_FIELD).equals("Radio")) {
                Log.v("RESPONSE", "About to create a radio button");
                // find
                LinearLayout content = (LinearLayout) view
                        .findViewById(R.id.genA_layout);
                // create
                TextView tv = new TextView(getActivity());
                RadioGroup rg = new RadioGroup(getActivity());
                rg.setOrientation(RadioGroup.HORIZONTAL);
                RadioButton rb = new RadioButton(getActivity());
                RadioButton rb2 = new RadioButton(getActivity());
                LinearLayout ll = new LinearLayout(getActivity());

                // set
                rb.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                rb2.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                ll.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                rb.setText(TAG_VALUE);
                rb2.setText(TAG_VALUE);
                tv.setText(map.get(TAG_NAME));
                ll.setOrientation(LinearLayout.HORIZONTAL);
                // add
                rg.addView(rb);
                rg.addView(rb2);
                ll.addView(tv);
                ll.addView(rg);
                content.addView(ll);
            }
            // else inflate the view as an EditText field
            else if (map.get(TAG_FIELD).equals("Text Field")) {
                Log.v("RESPONSE", "About to create an EditText");
                // find
                LinearLayout content = (LinearLayout) view
                        .findViewById(R.id.genA_layout);
                // create
                TextView tv = new TextView(getActivity());
                EditText et = new EditText(getActivity());
                LinearLayout ll1 = new LinearLayout(getActivity());
                // set
                tv.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                et.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                ll1.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                tv.setText(map.get(TAG_NAME));
                ll1.setOrientation(LinearLayout.HORIZONTAL);
                // add
                ll1.addView(tv);
                ll1.addView(et);
                content.addView(ll1);
            } else if (map.get(TAG_FIELD).equals("Check Box")) {
                Log.v("RESPONSE", "About to create a CheckBox");
                // find
                LinearLayout content = (LinearLayout) view
                        .findViewById(R.id.genA_layout);
                // create
                TextView tv = new TextView(getActivity());
                CheckBox cb = new CheckBox(getActivity());
                LinearLayout ll = new LinearLayout(getActivity());

                // set
                cb.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                ll.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                tv.setText(map.get(TAG_NAME));
                ll.setOrientation(LinearLayout.HORIZONTAL);
                // add
                ll.addView(tv);
                ll.addView(cb);
                content.addView(ll);

            } else if (map.get(TAG_FIELD).equals("Drop Down Menu")) {
                Log.v("RESPONSE", "About to create a Drop Down Menu");
            }

        }

        // find
        LinearLayout loader = (LinearLayout) view
                .findViewById(R.id.loader_layout);
        Button save = (Button) view
                .findViewById(R.id.generalAssets_save_button_ID);
        // set
        loader.setVisibility(View.GONE);
        save.setVisibility(View.VISIBLE);

    };
}

JSON

{
    "questions": [
        {
            "display_name": "Store #",
            "field_type": "Text Field",
            "option_value": ""
        },
        {
            "display_name": "Address",
            "field_type": "Text Field",
            "option_value": ""
        },
        {
            "display_name": "Type of Business",
            "field_type": "Drop Down Menu",
            "option_value": "Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther"
        },
        {
            "display_name": "Is this business good?",
            "field_type": "Radio",
            "option_value": "Yes\r\nNo"
        },
        {
            "display_name": "Are they nice people?",
            "field_type": "Check Box",
            "option_value": "Yes\r\nNo"
        }
    ],
    "success": 1
}

现在我希望它看起来像这样

Store # ------------------ <EditText>
Address ------------------ <EditText>
Is this business good? --- <RadioButton> (Yes) <RadioButton> (No)
Type of business? -------- <Spinner> (Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther)

等等,但是现在它正在这样做,从字面上看,这些是我看到的值。

Store# ------------------ nothing
Address ----------------- nothing
Is this business good?--- <RadioButton> (option_value) <RadioButton> (option_value)
Are they nice people?---- <CheckBox>

所以这可能比你需要看到的代码更多,但我想确保你完全理解发生了什么。

*编辑以显示更改

编辑后它的工作,有点......如果我需要更多帮助,我会发布另一个问题。原来的问题解决了。特别感谢@sarwar

4

2 回答 2

1

It looks like you're testing the wrong thing when you go to inflate your layout. This is where you get into trouble: for (String key : map.keySet()) {

You go and test all the keys in each map against the different types of fields. But there's no reason to have that inner loop at all. Your field_type is always contained in: map.get(TAG_FIELD) Likewise if you want the display_name, you can get it with map.get(TAG_NAME)Cut out the inner loop and compare your types to the correct values and you should be on the right track.

于 2013-06-04T23:28:54.010 回答
0

您没有正确地从 HashMap 调用它们

  map.put(TAG_NAME, name);
  map.put(TAG_FIELD, field);
  map.put(TAG_VALUE, value);

您只需调用类型..您需要在每个 UI 组件中显式调用名称和值..即

tv.setText(map.get(TAG_VALUE));   
于 2013-06-04T23:35:10.803 回答