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
  • 2
    It really seems like you should create a class called "ContactInfo" that has Name and Phone members. Why use arbitrary key/value stores when you can make your model concrete? Commented Jul 10, 2012 at 21:25

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much. Your answer to my question worked. as far as the suggestion goes, when I create a class as you said, how do I set values to the name and phone variables after i retrieve them from the query?
@Nerd add a constructor (or setter methods, but constructor is preferable if you need an immutable contact).
Updated a bit, hopefully that helps a bit :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.