2

I'm new on this Android thing and i dont know how to proceed from this point.

I would like to apply a rule to a button, the idea is simple. I got this button and when i press it, it has two options: it should check the code, if it matches then it start one Activity, if not, keep going as usual.

if it is the "same" as the data base, it goes to this new Activity

else it keep rolling the same class as it is.

also i don't know how to proceed with the execution of this class, how to call it in.

I reach to this code, but i got no idea how to proceed from here.

That is my code :

public void Send(View view) throws IOException {
    BdLocal bd = new BdLocal(this);

    if (bd.getCodeCompany() == "0A0A0A0A0A0A0A") {     
        Intent intent = new Intent(NewCompanyCodeActivity.this, NewCompCodeSetup.class);
        startActivityForResult(intent, 0);
    } else {
        //i dont know how to put the cass SendHug here
        //Object.getClass(SendHug);?  
    }

    class SendHug extends AsyncTask<String, Void, String>{
        ...
    }

Separated this Activitys and class work fine

Thanks!

Ketlin
  • 123
  • 3
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy Jul 29 '15 at 12:53
  • 1
    @KevinEsche the Sting comparison is iffy alright, but that's not the core of the question, so probably not a duplicate – ci_ Jul 29 '15 at 12:57
  • Instantiate a new instance of SendHug using the `new` operator. – stkent Jul 29 '15 at 12:58

2 Answers2

0

You need to change the if condition like this. And call your AsyncTask in else.

public void Send(View view) throws IOException {
        BdLocal bd = new BdLocal(this);
String text = bd.getCodeCompany();
        if ( text.equals( "0A0A0A0A0A0A0A")) {     
            Intent intent = new Intent(NewCompanyCodeActivity.this, NewCompCodeSetup.class);
            startActivityForResult(intent, 0);

        } else {
new SendHug().execute();

        }
Amsheer
  • 7,046
  • 8
  • 47
  • 81
0

The problem is here: bd.getCodeCompany() == "0A0A0A0A0A0A0A"

when you compare two String objects with the == comparator, you aren't comparing their value but rather, you are comparing their Object equality. Java checks to see if they are literally the same object, at the same memory location.

This will never be true when comparing a String literal such as "0A0A0A0A0A0A0A"

In order to compare the value of these objects you should call the .equals() method on the String.

Use of .equals() looks like "0A0A0A0A0A0A0A".equals(bd.getCodeCompany()).

By calling the method on the String literal you avoid a potential NullPointerException if it is possible for bd.getCompany() to return a null value.

You can call sendHug like: new SendHug().execute();

Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36