1. Are you ready for the Galaxy S20? Here is everything we know so far!

Simple String Handler Issue

Discussion in 'Android Apps & Games' started by silasc, Jun 3, 2010.

  1. silasc

    silasc Lurker
    Thread Starter

    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.

    Code (Text):
    1. @Override
    2.     public void onClick(View src) {
    3.             String s = (String)sp.getSelectedItem();
    4.             System.out.println("String");
    5.             System.out.println(s);
    6.             if (s == "7890"){
    7.                 startActivity(intent);
    8.             }
    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.

  2. Scythe

    Scythe Well-Known Member

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

    Code (Text):
    2. 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 (Text):
    2. if(s.equals("7890")) {
    To take it even further, if you intend on keeping the string static, you should define it as such.

    Code (Text):
    2. public static final String S_COMPARABLE = "7890";
    Code (Text):
    2. if(s.equals(S_COMPARABLE)) {
    silasc likes this.

Share This Page