0

I am experiencing something which is not an emergency problem as my application works however, but I really would like to understand why it is behaving like this.
There is in my program a method (findFilterInTable) with two string inputs (categoryInterval and paymentInterval).

The method is sending a request to my database, finding elements in some columns where categoryInterval = something and paymentInterval = something (depending on buttons clicked, choices of the user, and so on).

There is in my application a radiobutton setting the value of paymentInterval like this :

OnClickListener radiobutton_no_listener = new OnClickListener() {
    public void onClick(View v) {
        paymentInterval = null ; 
    }
};

OnClickListener radiobutton_yes_listener = new OnClickListener() {
    public void onClick(View v) {
        paymentInterval = " LIKE " + " '%visa%' " ; 
        }
};

So when the user clicks the button NO, it means paymentInterval is null.

Now, here is my method querying my database. As it was crashing while making a request in the COL_PAYMENT when paymentInterval was null, I want to change the query so : if paymentInterval is null, then it will not query the related column in my database (COL_PAYMENT). And else (if paymentInterval isn't null), it will query COL_PAYMENT and collect results.
I tried this way, it didn't work and in despair i just exchanged my statement and now it works :

 public Cursor findFilterInTable(String categoryInterval,
        String paymentInterval) {
    String where;
    if (paymentInterval != null) {
        where = "(" + COL_CAT1 + " IN " + categoryInterval + " OR "
                + COL_CAT2 + " IN " + categoryInterval + " OR " + COL_CAT3
                + " IN " + categoryInterval + ") AND (" + COL_PAYMENT
                + paymentInterval + ") ";
    }

    else {
        where = COL_CAT1 + " IN " + categoryInterval + " OR " + COL_CAT2
                + " IN " + categoryInterval + " OR " + COL_CAT3 + " IN "
                + categoryInterval;
    }

    Cursor c = myDatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID,
            COL_NAME, COL_STREET, COL_WEBSITE, COL_PAYMENT, COL_TELEPHONE,
            COL_PRICE, COL_REMARKS, COL_DATEFRIENDLY }, where, null, null,
            null, null);
    return c;
}

This code works and do this opposite of what is written. When I use my application:
- if paymentInterval is null (the user clicks NO), it doesn't query the COL_PAYMENT
- if paymentInterval isn't null (and is like %visa% in my case), it does query the database and display the right results.

Do you know what causes this weird results ?

[edit] listener attached to my buttons :

radiobutton_yes = (RadioButton) dialogView
                .findViewById(R.id.radiobutton_yes);
        radiobutton_yes.setOnClickListener(radiobutton_yes_listener);

        radiobutton_no = (RadioButton) dialogView
                .findViewById(R.id.radiobutton_no);
        radiobutton_no.setOnClickListener(radiobutton_no_listener);
Phalanx
  • 1,217
  • 6
  • 25
  • 36
  • Where is the code where you actually attach the click listener to the buttons? – brianestey Mar 27 '13 at 07:38
  • Gosh maybe in our earlier discussion, I means not null and write it as null – Hoan Nguyen Mar 27 '13 at 07:43
  • Ahah don't worry, I made some research but everytime i read ( myString != null) presented as answer to people asking for a way to set a string to null, but it wasn't clear that it was the opposite! Thanks anyway, it's working well now ! – Phalanx Mar 27 '13 at 07:45

1 Answers1

2

This code does exactly what is written.

if (paymentInterval != null)

!= is the is not equal operator. So, if paymentInterval is not equal to null, it adds COL_PAYMENT to the query; else it leaves it out.

j__m
  • 9,392
  • 1
  • 32
  • 56
  • 1
    This. Maybe you used if(paymentInterval = null) in your first try? Instead of == – Stefan de Bruijn Mar 27 '13 at 07:42
  • Geez really ?! I have been reading about this here and all I read led me to think the opposite as it was never clearly stated ( http://stackoverflow.com/questions/5499809/how-to-check-a-string-is-not-null ). Thanks a lots for the correction ! =) – Phalanx Mar 27 '13 at 07:43