I have an arraylist called fetchContactName()
which returns all names (31) in my contact list, an arraylist called fetchContactNumbers()
which returns all phone numbers (31) associated with fetchContactName()
, and an arraylist called newList
which has all the numbers that sent me text messages (6).
I was trying to change the phone numbers in newList
by looking for the same numbers in fetchContactNumbers()
. When there was a match, I would then index the position in fetchContactNumbers()
and use that index to get the name at the same position in fetchContactName()
. From there, I would replace the numbers in newList
with the names in fetchContactName()
according to the indexed position.
This is an example I've made that executes as stated above:
The problem with the above example is that when I apply it to my listview
, only one element in the list is changed to a name. More oddly, newList.get(3)
is the only one that changed to a name. Not newList
at positions 0, 1, 2, 4 and 5
.
From others suggests, how can I use a HashMap
to give the same expected result as the example given above? I was told to use fetchContactNumbers()
and fetchContactNames()
in the Hashmap
then use the HashMap
to compare with newList
, but I cant find any source to achieve this.
UPDATE:
Here is the working code for my HashMap, but the results are still the same as above:
for (String str : fetchContactNames()) {
contactNameNew += str + " ";
}
for (String str2 : fetchContactNumbers()) {
contactNumberNew += str2 + " ";
}
//String to split
String temp[];
String temp2[];
String delimiter = " ";
temp = contactNameNew.split(delimiter);
temp2 = contactNumberNew.split(delimiter);
HashMap<String, String> contacts = new HashMap<String, String>();
contacts.put(contactNameNew, contactNumberNew);
for(int i = 0; i < fetchContactNames().size(); i++){
contacts.put(temp[i], temp2[i]);
}
Iterator<Entry<String, String>> iterator = contacts.entrySet().iterator();
while(iterator.hasNext()){
Entry<String, String> entry = iterator.next();
int position = getPosition(newlist, entry.getValue());
if (newlist.contains(entry.getValue())) {
newlist.set(position, entry.getKey());
}
}