ArrayList in JAVA/Android, how to print only the value

Rocco Galati
Rocco Galati used Ask the Experts™
on
I have a contactList = new ArrayList<>(); where I store information in this format: "name", value_for_name.

I populate my contactList inside this function:

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;
        }

Open in new window


The problem is that when I try to print a value:

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

Open in new window


The output is in this format: {name="Foo"}

I only want to print Foo I tried also with:
System.out.println(String.valueOf(contactList.get(position)));

Open in new window


but I always get the whole string: {name="Foo"}

Can you help me, please? Do I really need to parse the string?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Commented:
Edit: Just a guess, but I think contactList.get(position) returns the whole object (Map), instead of a specific key within that Map.  What happens if you grab the key "name" instead?

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

Open in new window


or ...

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

Open in new window

Rocco GalatiR&D Engineer

Author

Commented:
it worked very well!!! thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial