Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Strings issue

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:

  1. It prints out exactly correctly (including checking for spaces in front and at the end).
  2. I have tried explicitly casting the received list elements as (String) just to be sure they are Strings.
  3. If I run "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.

like image 402
user3113666 Avatar asked Dec 21 '13 19:12

user3113666


1 Answers

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.

like image 143
Wesley Avatar answered Oct 14 '22 21:10

Wesley