2

I am using contents in one ArrayList(String) to be used to update another ArrayList(EditText). I want to loop through the String array, get its detail and use it to setText for each corresponding EditText in the ArrayList(EditText).

So String at index 0 will be used to edit EditText at index 0 and so on. I was trying to use a double For loop but it ends filing all the EditTexts as "quantity". I understand why, but is there even a way to loop through this to do this? Or I need to find a different method?

ArrayList<String> detailArray = new ArrayList<>(Arrays.asList(
                "category", "name", "brand", "quantity"));
ArrayList<EditText> emptyFields = new ArrayList<>(Arrays.asList(
                category, name, brand, quantity));

for(EditText x : emptyFields){
    for(String y : detailArray){
        x.setText(y);
    }
}
2
  • 2
    If the lists are the same size you don't need 2 loops. Use a regular for loop then just use the the loop variable in both get and set methods. Commented Dec 15, 2015 at 0:37
  • @takendarkk I clearly overcomplicated it.. Thanks. Commented Dec 15, 2015 at 0:42

2 Answers 2

3

Use this

for(int i = 0; i < emptyFields.size(); i++) {
    emptyFields.get(i).setText(detailArray.get(i));
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you trust both ArrayLists have same sizes, use this

for(EditText x : emptyFields){
    x.setText(detailArray.get(emptyFields.indexOf(x))));
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.