3

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:

http://pastebin.com/pApHNkXa

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

2 Answers2

0

I think it will be better if I changed your code and answered...

import java.util.ArrayList;
import java.util.HashMap;

public class StringT {


public static void main(String[] args) {

    // alpha would represent fetchContactNames()
    ArrayList<String> alpha = new ArrayList<String>();
    // bravo would represent fetchContactNumbers()
    HashMap<String, String> bravo = new HashMap<String, String>();
    // charlie would represent newList
    ArrayList<String> charlie = new ArrayList<String>();

    bravo.put("1", "One"); charlie.add("5");
    bravo.put("2", "Two"); charlie.add("1");
    bravo.put("3", "Three"); charlie.add("3");
    bravo.put("4", "Four"); charlie.add("4");
    bravo.put("5", "Five"); charlie.add("2");

    // this will print charlie just as you see above
    for(int i = 0; i < charlie.size(); i ++){
        System.out.println(charlie.get(i));
    }

    System.out.println("---------------------");

    // this will print charlie with the numbers replaced as names
    for(int i = 0; i < charlie.size(); i++){
        String value;
        if((value = bravo.get(charlie.get(i))) != null){
            charlie.set(i, value);
        }
    }

    for(int i = 0; i < charlie.size(); i ++){
        System.out.println(charlie.get(i));
    }
}
}
vellvisher
  • 484
  • 4
  • 12
  • I dont see how to implement this in my code. I have 2 arraylist for the hashmap and I'm being told that I should convert those to Strings, then compare it to my newList ArrayList –  Jun 11 '12 at 05:36
  • I've adapted your code and replaced it..also, I choose the numbers to be the "keys" so that you get fast access when searching according to the numbers. If you want to search by a person's name and get his contact numbers then the name is a better "key". Hope it helps! – vellvisher Jun 11 '12 at 05:56
  • also, could you tell more on what "listView" means in your post if its relevant... – vellvisher Jun 11 '12 at 05:58
  • Your "if" statement isnt correct and if you havent worked with listviews, then it will be hard to understand the problem. The problem I'm having is that only one out of six changes to a name. It seems like the code is only running through itself checking one thing, then stops. –  Jun 11 '12 at 08:42
  • hmm..as far as I understood your problem, I think the "if" is correct..also produces the same output as your pasted code.. anyway, maybe the problem with listView is that it isn't updating..[link]http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview might help since i've worked with iOS lists mostly and the problem generally was with refreshing.. – vellvisher Jun 11 '12 at 09:02
0

your code is working that u placed here http://pastebin.com/pApHNkXa i tried it in android and its working alright its giving output as five, two, seven, three .

samir
  • 339
  • 6
  • 19
  • For some reason, it's not working in my listview. I'm creating an app that mimics the SMS inbox. Right now, it is displaying the phone numbers of contacts on my phone, but I'm trying to replace them with the actual contact names. I dont know why, but its just not working correctly for me and there are no errors. –  Jun 11 '12 at 08:29