0

I have a ListView with an arraylist with an unknown number of string elements. I want to change/modify every single one of these string items. The problem is that i dont know how many items there are since the user can change it.

I have a translate function that takes a string and returns a string. What i want to do is

arraylistelement1 = translate(arraylistelement1);
arraylistelement2 = translate(arraylistelement2);
... 

and repopulate the listview arraylist with the new strings.

Whats a way to do this?

2
  • How do i do it without knowing the number of elements? Commented Oct 14, 2013 at 22:18
  • Something like (for int i=0; i< mArrayList.size(); i++){...} Commented Oct 15, 2013 at 7:34

2 Answers 2

2

Iterate over the list and create a new list of translated options from the original then replace the contents of the original list with the new values. If you do the replacing while iterating you'll get ConcurrentModificationExceptions.

Sign up to request clarification or add additional context in comments.

3 Comments

ArrayList<String> temp = new ArrayList<String>(); for(String string : strings){ temp.add(translate(string)); } strings = temp;
@MuratKaya He just did!
yes didnt see it before. I couldnt get the code to work though. I put my arraylist instead of strings. Do I replace string with anything?
2

Use ListIterator.set:

public static void main(String[] args) {
    List<String> list = new ArrayList<>(Arrays.asList("s0", "s1", "s2"));

    ListIterator<String> iter = list.listIterator();

    while (iter.hasNext())
       iter.set(translate(iter.next()));

    for (String element : list)
        System.out.println(element);
}

public static String translate(String element) {
    return element + " " + Math.random();
}

Comments

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.