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);