• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 536
  • Last Modified:

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

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?
0
Rocco Galati
Asked:
Rocco Galati
1 Solution
 
_agx_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

0
 
Rocco GalatiR&D EngineerAuthor Commented:
it worked very well!!! thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now