来自HashMap的视图未正确命名

时间:2013-06-04 22:39:07

标签: java android json

我正在尝试从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

2 个答案:

答案 0 :(得分:1)

当你去扩充布局时,看起来你正在测试错误的东西。这就是你遇到麻烦的地方:for (String key : map.keySet()) {

您可以根据不同类型的字段测试每个地图中的所有键。但是没有理由拥有那个内循环。您的field_type始终包含在: map.get(TAG_FIELD)同样,如果你想要display_name,你可以通过map.get(TAG_NAME)获得它。剪切内部循环并将你的类型与正确的值进行比较,你应该走在正确的轨道上。

答案 1 :(得分:0)

你没有正确地从HashMap中调用它们

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

你只需要调用类型..你需要在每个UI组件中明确地调用名称和值..即

tv.setText(map.get(TAG_VALUE));