ArrayList,只打印值

问题描述 投票:0回答:1

我有一个contactList = new ArrayList<>();,我以这种格式存储信息:"name", value_for_name

我在这个函数中填充了我的contactList:

protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);



            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    JSONArray contacts = jsonObj.getJSONArray("results");

                    // looping through All Results
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String icon = c.getString("icon");
                        String id = c.getString("id");
                        String name = c.getString("name");



                        // tmp hash map for single contact
                        HashMap<String, String> contact = new HashMap<>();

                        // adding each child node to HashMap key => value
                        // contact.put("id", id);
                        contact.put("name", name);
                        // contact.put("email", icon);


                        // adding contact to contact list
                        contactList.add(contact);

                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }

问题是当我尝试打印一个值时:

System.out.println(contactList.get(position));

输出采用以下格式:{name =“Foo”}

我只想打印Foo我也试过:System.out.println(String.valueOf(contactList.get(position)));但我总是得到整个字符串:{name="Foo"}

你能帮我吗?我真的需要解析字符串吗?

java android arraylist
1个回答
2
投票

尝试:

System.out.println(contactList.get(position).get("name"));

我看到你有一个数组列表的hashmap,所以你想从数组中的“X”位置获取对象,之后通过属性名从hashmap获取值。

© www.soinside.com 2019 - 2024. All rights reserved.