1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

Simple String Handler Issue


Last Updated:

  1. silasc

    silasc New Member This Topic's Starter

    Joined:
    May 29, 2010
    Messages:
    1
    Likes Received:
    0
    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 (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.
     

    Advertisement
  2. Scythe

    Scythe Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    106
    Likes Received:
    27
    You need to brush up on your Java skills a tad.

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

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

Share This Page

Loading...