gpt4 book ai didi

java - 来自 HashMap 的 View 未正确命名

转载 作者:行者123 更新时间:2023-11-30 03:33:58 25 4
gpt4 key购买 nike

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

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) 去掉内部循环并将您的类型与正确的类型进行比较值(value)观,你应该在正确的轨道上。

关于java - 来自 HashMap 的 View 未正确命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16928672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com