• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Simple String Handler Issue

silasc

Lurker
Hello, my name is Silas. I am new to android development. I have experience with programming, though new to Android. I am finding it very great though! Thank you for your response ahead of time.

Q1: I am trying to have my program grab web content based on "Spinner" selection. I have a "Spinner" and a "button" to execute selected item.

ie:
Code:
@Override
    public void onClick(View src) {
            String s = (String)sp.getSelectedItem();
            System.out.println("String");
            System.out.println(s);
            if (s == "7890"){
                startActivity(intent);
            }
I am debugging my string "s" and it shows "7890" for example. My IF command does not respond to the selection.

If I remove the IF then it executes just fine. I have also tried if ((String)s == "7890") {} with no success. Any help would be great.


Q2: Do you know of a good example to parse text from a web-page and populate text fields within my UI?

Thank you very much.

Good Night.
 
You need to brush up on your Java skills a tad.

Code:
if(s == "7890") {
That will always be false. s is not in the same spot in memory as the newly declared string "7890". You're comparing address, not text.

What you want to do is the following:

Code:
if(s.equals("7890")) {
To take it even further, if you intend on keeping the string static, you should define it as such.

Code:
public static final String S_COMPARABLE = "7890";
Then
Code:
if(s.equals(S_COMPARABLE)) {
 
Back
Top Bottom