gpt4 book ai didi

java - 从 ArrayList 内的 HashMap 创建 View

转载 作者:行者123 更新时间:2023-12-01 23:46:50 25 4
gpt4 key购买 nike

这是一个我似乎无法理解的逻辑问题,但我认为我已经很接近了。我从 JSON 响应中获取值并将它们存储在 HashMap 中,然后将 HashMap 添加到 ArrayList

doInBackground

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);
for (String key: map.keySet()) {
System.out.println("key : " + key);
System.out.println("value : " + map.get(key));
}
infoList.add(map);
}

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

现在上面的 for 循环 从我的 JSON 打印以下内容

06-03 19:35:29.928: I/System.out(9691): key : option_value
06-03 19:35:29.928: I/System.out(9691): value :
06-03 19:35:29.928: I/System.out(9691): key : field_type
06-03 19:35:29.928: I/System.out(9691): value : Text Field
06-03 19:35:29.928: I/System.out(9691): key : display_name
06-03 19:35:29.928: I/System.out(9691): value : Store #
06-03 19:35:29.928: I/System.out(9691): key : option_value
06-03 19:35:29.928: I/System.out(9691): value :
06-03 19:35:29.928: I/System.out(9691): key : field_type
06-03 19:35:29.928: I/System.out(9691): value : Text Field
06-03 19:35:29.928: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Address
06-03 19:35:29.938: I/System.out(9691): key : option_value
06-03 19:35:29.938: I/System.out(9691): value : Education
06-03 19:35:29.938: I/System.out(9691): Health
06-03 19:35:29.938: I/System.out(9691): Computers
06-03 19:35:29.938: I/System.out(9691): Food
06-03 19:35:29.938: I/System.out(9691): Retail
06-03 19:35:29.938: I/System.out(9691): Other
06-03 19:35:29.938: I/System.out(9691): key : field_type
06-03 19:35:29.938: I/System.out(9691): value : Drop Down Menu
06-03 19:35:29.938: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Type of Business
06-03 19:35:29.938: I/System.out(9691): key : option_value
06-03 19:35:29.938: I/System.out(9691): value : Yes
06-03 19:35:29.938: I/System.out(9691): No
06-03 19:35:29.938: I/System.out(9691): key : field_type
06-03 19:35:29.938: I/System.out(9691): value : Radio
06-03 19:35:29.938: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Is this business good?
06-03 19:35:29.938: I/System.out(9691): key : option_value
06-03 19:35:29.938: I/System.out(9691): value : Yes
06-03 19:35:29.938: I/System.out(9691): No
06-03 19:35:29.938: I/System.out(9691): key : field_type
06-03 19:35:29.938: I/System.out(9691): value : Check Box
06-03 19:35:29.938: I/System.out(9691): key : display_name
06-03 19:35:29.938: I/System.out(9691): value : Are they nice people?

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
}

现在在 onPostExecute() 中,我需要循环遍历名为“infoList”的 ArrayList,然后获取 HashMap 值。根据该信息,我需要创建 View 。所以我的代码应该看起来像这样,但我无法正确执行。

protected void onPostExecute(String string) {
// dismiss the dialog
pDialog.dismiss();
for (int i = 0; i < infoList.size(); i++) {
// get HashMap, how? i.toString()?
for (String key: map.keySet()) {
if (map.get(key).equals("Radio")) {
//create RadioButtons, setTexts to option_value values
} else if (map.get(key).equals("Text Field")) {
//create EditText
} else if (map.get(key).equals("Check Box")) {
//create CheckBox's, setTexts to option_value values
} else if (map.get(key).equals("Drop Down Menu")) {
//create Spinner, place option_value values into array and populate
}
}
}

那么我是否让事情变得比需要的更困难了?我觉得他们一定是一种更简单的方法。如果没有,我将非常感谢编写此 for 循环 的帮助。

编辑回复评论

我希望它能够在 fragment 中膨胀一个布局,如下所示

Store # ------------------ <EditText>
Address ------------------ <EditText>
Is this business good? --- <RadioButton>

等等。 RadioButtonCheckbox 将通过可选值设置其文本

完成后,我会将其发送回我最初从中获取此信息的数据库。

最佳答案

为您的问题构建一个 POJO 类:

Class Questions{
private String Name;
private String field_type;
private ArrayList option_value;
<Getter and Setters>
}

将其用作 POJO 类。并将其添加到 ArrayList 中。

仅在需要时才使用 map 。使用 POJO/Business Objects 传输数据就足够了,而且是最适合的。

请告诉我你的想法。

关于java - 从 ArrayList 内的 HashMap 创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907756/

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