这是一个我似乎无法解决的逻辑问题,但我想我已经接近了。我从JSON
响应中获取值并将它们存储在 a中,HashMap
然后将它们添加HashMap
到ArrayList
.
做背景
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 loop
从我的打印以下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()
我需要遍历我ArrayList
命名的“infoList”,然后获取HashMap
值。基于该信息,我需要创建Views
. 所以我的代码应该看起来像这样,但我做错了。
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 loop
。
根据评论进行编辑
我希望这可以膨胀一个fragment
看起来像这样的布局
Store # ------------------ <EditText>
Address ------------------ <EditText>
Is this business good? --- <RadioButton>
等等等等。RadioButton
orCheckbox
将由 optional_values 设置它们的文本
完成后,我会将其发送回最初从中获取的数据库。