My program produces a list of Strings in one activity, then passes it to another activity, and the second activity uses the strings.
When I test by printing out each element of the list at the beginning of the second activity, the printout looks perfect. For example, if I am hoping for the list to contain "Lemon Juice"
, it prints out exactly right, but yet the logic in the second activity still doesn't work. If I add "Lemon Juice"
just like that to the list manually, the logic in the second activity works fine, so the issue is that somehow the string in the received List is not really "Lemon Juice"
. But:
"Lemon Juice".contains(received String)
it comes back true, and if I run received String.contains("Lemon Juice")
it
comes back true
, but if I run received String.equals("Lemon Juice")
it comes back false. This is very confusing to me.Can anyone think of a possible explanation for how something that is cast as a string, prints as a string, and looks like a string, is not performing like a string?
EDIT to include some code as requested:
// instance variable at top of class--list to which strings will be added for use in
// 2nd activity
private List<String> exs = new ArrayList<String>();
// get array of strings from extra from intent from first activity
String[] recExs = getIntent().getStringArrayExtra(BrowseActivity.EXS);
for (int exx = 0; exx < recExs.length; exx++) {
String curEx = (String) recExs[exx];
exs.add(curEx);
}
Somehow, when I pass exs to the method I need to use the strings, it doesn't work, even though, as explained above, printing and calling contains etc all show that the string, before I added it to exs, was at I wanted it to be.
It's hard to help when you only posted a small snippet of your code.
But I'm guessing the reason String.contains
works but String.equals
doesn't is that maybe there is space in the Strings. Try String.trim
on both side of the Activity when passing and receiving data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With