本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

HashMap 中的视图命名不正确

发布于2024-12-03 08:59     阅读(1022)     评论(0)     点赞(1)     收藏(5)


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


解决方案


看起来您在扩展布局时测试了错误的内容。这就是您遇到麻烦的地方:for (String key : map.keySet()) {

您去测试每个映射中的所有键是否与不同类型的字段相匹配。但是根本没有理由使用那个内部循环。您的 field_type 始终包含在:map.get(TAG_FIELD)同样,如果您想要 display_name,您可以通过map.get(TAG_NAME)切断内部循环并将您的类型与正确的值进行比较来获得它,这样您就应该走在正确的轨道上了。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/694051/74c26f1c4e570bc0e032/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)