- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个我似乎无法理解的逻辑问题,但我认为我已经很接近了。我从 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>
等等。 RadioButton
或 Checkbox
将通过可选值设置其文本
完成后,我会将其发送回我最初从中获取此信息的数据库。
最佳答案
为您的问题构建一个 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/
我有不同的结构,它们都包含一个 HashMap与 String作为键,但具有不同的值类型。例如,一个结构有一个类型为 HashMap 的成员, 另一个将有一个 HashMap 类型的成员, 等等。 我
我想制作一个包含学生姓名和科目的板,每个学生在每个科目中都有一个成绩(或者没有..他可以离开考试而不写,然后他的案子将是空的)。我只想使用 HashMap。我的意思是,它会是这样的: HashMap>
是否有内存和速度高效的方法来在 HashMap 中动态存储唯一键:值对? key 保证是唯一的,但它们的数量经常变化。插入和删除必须很快。 我所做的是包含有符号距离场的八叉树(非线性/完整)。八叉树经
有谁知道为什么选择通过 LinkedList 而不是另一个 Hashmap 来实现 HashMap 的存储桶。如果桶本身变成了 HashMap,那么 contains 或 get 的时间复杂度似乎是
我想创建一个具有嵌套结构的 HashMap,就像这个复杂的示例: { type: boy name: Phineas father: type: man
这个问题在这里已经有了答案: How do I create a global, mutable singleton? (7 个答案) 关闭 7 年前。 我想要一个可扩展的字典,将 Object 与
HashMap> hm = new HashMap>(); hm.put("Title1","Key1"); for(int i=0;i hm1 = new H
我必须修改当前代码以适应 Spring MVC。我有 HashMap hashmap = new HashMap(); request.setAttribute("dslrErrors", hashm
我正在尝试进行一些错误捕获。 错误应该检查数组的长度是否小于 2,并检查 HashMap 是否包含用户输入的键。 捕获的错误必须仅使用 if 语句,并且必须使用 .length() 方法,并且必须使用
在 stackoverflow 上提出另一个问题后,(Java- Why this program not throwing concurrent Modification exception)我开始
我有两个类,想使用 org.dozer.Mapper( http://dozer.sourceforge.net/ ) 将 Female 对象的属性映射到 Male 对象。 第一类是: public
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
是否有任何方法可以检查 HashMap 是否包含一组特定的键(这些键是在数组中给出的)。当我尝试类似下面的代码时,它返回 false。 map.containsKey(arrayOf("2018-01
跟进我的问题:How To Access hash maps key when the key is an object 我想尝试这样的事情:webSearchHash.put(xfile.getPa
我有一个可扩展的 ListView ,对于每个 child ,我需要有 4 个“额外”或字符串或其他名称来调用它:- 子标题- 描述- 链接1- 链接2 跟着教程,创建 ListView 、不同的 p
我想确保这是正确的,因为如果不正确,它可能会破坏我的应用程序。 我有这个: private static HashMap> balance = new HashMap<>(); 如果我得到这样的值:
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我是一名优秀的程序员,十分优秀!