簡體   English   中英

從ArrayList中的HashMap創建視圖

[英]create Views from HashMap inside ArrayList

這是一個邏輯問題,我似乎無法解決,但我我已經接近了。 我從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 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()我需要遍歷名為“ infoList”的ArrayList ,然后獲取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>

等等RadioButtonCheckbox的文本將由optional_values設置

完成此操作后,我會將其發送回我最初從中獲得的數據庫。

為您的問題構造一個POJO類:

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

將此用作POJO類。 並將其添加到ArrayList中。

僅在需要時使用地圖。 使用POJO / Business Objects進行數據傳輸就足夠了,最適合。

請讓我知道你的想法。

與使用不合適的庫相比,這會讓您自己更難。

使用Jackson ,您可以輕松完成自己想做的事情。 首先,從輸入源獲取JsonNode ,這通常是通過ObjectMapper完成的:

// "mapper" is an already initialized ObjectMapper
final JsonNode response = mapper.readTree(yourURL); // catch, treat IOException

現在,閱讀success

if (response.get("success").intValue() != 1 )
    // deal with failure

成功? 處理所有值:

for (final JsonNode question: response.get("questions"))
    // do something with "question", which is an element of the "questions" array

請參閱JsonNode的文檔。 根本沒有更好的類設計用來訪問/導航Java中的JSON。

之后,如果需要/想要,可以使用批注將每個問題反序列化為POJO並進行處理。 這是我為JSON補丁操作所做的示例:

https://github.com/fge/json-patch/blob/master/src/main/java/com/github/fge/jsonpatch/JsonPatchOperation.java

該文件將根據"op"成員值是什么而生成JsonPatchOperation的不同實現。 例如:

{ "op": "remove", "path": "/foo" }

將生成一個RemoveOperation

首先,傑克遜似乎很艱巨。 但是一旦您知道如何使用它,就輕而易舉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM