简体   繁体   中英

Trying to iterate through a list

Im trying to make a iterating list where it will set the text to "Copper 60 = Silver 30" So the "Copper" and "Silver" are strings then the 60 comes from the editText and the 30 comes from the converted cost. At the moment it is displaying "Copper 30 = Silver" So it is displaying it the wrong way and not adding in the 60 from the ediText. Can someone please help ive been stuck on it for hours. Here is my code:

            myList.add(convertedCost.getText().toString());
            editText.getText();
            editText.setText("");
            String copperStr = "Copper";
            String silverStr = "Silver";
            for(String editText : myList){
                copperStr = copperStr + " " + editText + " = " + silverStr;
            }
            txtList.setText(copperStr);

        }
    });

The main problem with this is that myList, by the time you get to the for loop, only contains a single element: 30, from the convertedCost.getText().ToString(). At no point is the 60 for the copper value added to myList. When you go through the for loop, editText is assigned to the next element in myList, starting with the first. The only element in myList, being 30, means that in the first loop, the variables are as follows:

myList is ["30"]
editText is myList[0] is "30"
copperStr is "Copper"
silverStr is "Silver"

So that when you complete your first loop, the following is assigned to copperStr:

copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper" + " " + "30" + " = " + "Silver"
copperStr = "Copper 30 = Silver"

But if you inserted "60" into the list, before you inserted "30", then the first loop would have the following variables set

myList is ["60", "30"]
editText is myList[0] is "60"
copperStr is "Copper"
silverStr is "Silver"

And the first loop would produce the following:

copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper" + " " + "60 + " = " + "Silver"
copperStr = "Copper 60 = Silver"

Then by the second loop, the variables would be as follows:

myList is ["60", "30"]
editText is myList[1] is "30"
copperStr is "Copper 60 is Silver"
silverStr is "Silver"

And the second loop would produce the following result:

copperStr = copperStr + " " + editText + " = " + silverStr
copperStr = "Copper 60 = Silver" + " " + "30" + " = " + "Silver"
copperStr = "Copper 60 = Silver 30 Silver"

Which is also not what you want

You may instead wish for something akin to the following

myList.add("60");
myList.add(convertedCost.getText().toString());
editText.getText();
editText.setText("");
String copperStr = "Copper";
String silverStr = "Silver";
editText.setText(copperStr + " " + myList[0] + " = " + myList[1] + " " + silverStr);
txtList.setText(editText);

First, you are reusing variable name editText , so you are losing access to it. But that doesn't matter since you are setting it to an empty string anyway.

Second, according to your code, editText and convertedCost are the exact same thing, since you are adding the convertedCost to the myList and then reading from that list as the editText .

Third, you are not getting the latter number 60 because you are not concatenating it to the string.

May be this code will help you out:

copperStr = String.format("Copper %s = Silver %s", editText, convertedCost.getText().toString());

This is kinda like concatenating several strings with + but doing it in a more readable fashion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM