Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8214601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:30:49+00:00 2026-06-07T11:30:49+00:00

I have a globally defined ArrayList as ArrayList<Map<String, String>> mContactList; . Now, in one

  • 0

I have a globally defined ArrayList as ArrayList<Map<String, String>> mContactList;.

Now, in one of the methods, I am defining a HashMap like this:

Map<String, String> NamePhoneType = new HashMap<String, String>();    
NamePhoneType.put("Name", contactName); //contactName is returned from a query
NamePhoneType.put("Phone", phoneNumber); //phoneNumber is returned from a query
mContactList.add(NamePhoneType);

So mContactList now looks like [{Name=Abc, Phone=123}, {Name=def, Phone=456}...]

Now when I try to iterate over the mContactList using the following, I always get the key as 'Phone' and Value as '123' or '456' depending on the index.

public void onItemClick(AdapterView<?> av, View v, int index,long arg) {
    Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
    Iterator<String> myVeryOwnIterator = map.keySet().iterator();
    while (myVeryOwnIterator.hasNext()) {
        String key = (String) myVeryOwnIterator.next();
        String value = (String) map.get(key);
        getNumber.setText(value);                                    
        }               
    }
});

I want the Name and the Phone of the selected index in 2 separate strings. How do I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:30:50+00:00Added an answer on June 7, 2026 at 11:30 am

    Instead of having a Map as you do, I would recommend creating a class called Contact. Your ArrayList would then be declared as

    ArrayList<Contact> mContactList;
    

    The Contact class would look like

    public class Contact {
        private String name;
        private String phoneNumber;
    
        public Contact (String name, String phoneNumber) {
            this.name = name;
            this.phoneNumber = phoneNumber;
        }
    
        public String getName() {
            return name;
        }
    
        public String getPhoneNumber() {
            return phoneNumber;
        }
    }
    

    Whenever you end up with nested data structures, it’s usually a sign to rethink your design as there’s usually a better way to do it.


    However, to answer your question; note what this piece of code is doing.

    Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
    Iterator<String> myVeryOwnIterator = map.keySet().iterator();
    while (myVeryOwnIterator.hasNext()) {
        String key = (String) myVeryOwnIterator.next();
        String value = (String) map.get(key);
        getNumber.setText(value);                                    
    }
    

    You are iterating through a map which (appears to) always have two pairs:

    "Name", nameValue
    "Phone", phoneValue
    

    There’s no need to iterate through the Map to get the values you desire. Your code should look like:

    Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
    String phoneNumber = map.get("Phone");
    String name = map.get("Name");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined globally a list in one class as a pointer: class1.cpp type
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
My computer have a proxy server defined globally (in internet options configuration). I have
Is there a way to globally declare a #define? Like I want to have
I've set up company to load globally like this: (let ((company-path path/to/company)) (add-to-list 'load-path
I have a groovy script, Bootstrap.groovy, where I have defined several metaclass methods on
I have an OpenGL shader written in glsl. I have a globally defined array
I have a scenario where I have a globally available Properties window (similar to
I have setup background image for a button as below. // declarations globally declared...
have written this little class, which generates a UUID every time an object of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.