Reputation: 1217
I'm working on an application and have a little problem right now. Let me explain what is the purpose of this application:
- there is a main menu with different buttons (let's say 16 buttons).
- each time the user clicks on a button, it sends a request to a database and display a listview of results with different results depending on the button you clicked.
- in the database, there is a column with a "category number". (actually there are 3 columns with CatNumbers but let's keep it simple).
- the request asks to display all the elements where this category number equals some values (these are the inputs of my method, called "myVariableN" sent from the MainActivity to the ListView activity via putExtra/getExtra). In my ListViewActivity which displays the results:
private void displayListView() {
Bundle bundle = getIntent().getExtras();
String title = bundle.getString("title", "Choose here :");
int myVariable1 = bundle.getInt("myVariable1", 500);
int myVariable2 = bundle.getInt("myVariable2", 500);
int myVariable3 = bundle.getInt("myVariable3", 500);
int myVariable4 = bundle.getInt("myVariable4", 500);
int myVariable5 = bundle.getInt("myVariable5", 500);
int myVariable6 = bundle.getInt("myVariable6", 500);
So what I have so far is this : if you click on button 1 for example, it sends 6 variables (myVariable1, 2, 3 ... 6) and ask to display all elements "WHERE CatNumber IN (myVariable1, myVariable2, ... myVariable6).
public Cursor findPoiInTable(int myVariable1, int myVariable2,
int myVariable3, int myVariable4, int myVariable5, int myVariable6)
{
String inInterval = "(?,?,?,?,?,?)";
String where = COL_CAT1 + " IN " + inInterval + " OR " + COL_CAT2
+ " IN " + inInterval + " OR " + COL_CAT3 + " IN " + inInterval;
String[] whereArgs = new String[3 * 6];
for (int i = 0; i < 3; i++)
{
whereArgs[6 * i] = Integer.toString(myVariable1);
whereArgs[6 * i + 1] = Integer.toString(myVariable2);
whereArgs[6 * i + 2] = Integer.toString(myVariable3);
whereArgs[6 * i + 3] = Integer.toString(myVariable4);
whereArgs[6 * i + 4] = Integer.toString(myVariable5);
whereArgs[6 * i + 5] = Integer.toString(myVariable6);
}
Cursor c = myDatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID,
COL_NAME, COL_STREET}, where, whereArgs, null, null, null);
return c;
}
Problem is, out of these 16 buttons from the main menu, there are a few buttons which would require to get data from the database with a much bigger range of category numbers. For example, something like "SELECT .... FROM.... WHERE CatNumber IN (myVariable1, myVariable2, ..... myVariable*50*).
And unfortunately, the method must be the same for every button, no matter if there are 2 or 50 input variables.
I feel like if I do this, my method is gonna be too heavy and will slow down my whole application, because in most of the cases, even when the request will be supposed to find 2 or 3 CatNumbers, the method will be defined for 50 inputs or more so it will have to make the whole calculation every time.
What I was thinking about was:
a/ Modifying my method with a IF statement, so for example, IF there are less than... let's say 6 variables sent, it will make the light method with only 6 myVariable input, ELSE, it's gonna run the heavy method with 50 inputs. So the heavy method will be used only in a few cases, the wrong thing is that even if there are only 7 inputs, it will run the heavy method. Or in the same kind of idea, I could send a boolean sent via putExtra and set to TRUE (for using the light method) and FALSE (and then it would use the heavy method).
b/ Set the value by default to 500 (as done in my code), so only the specific myVariables have a value different than 500. And then my question is: is it possible to define a method with 50 inputs and to make it only use the input whose value is different from a certain number, so the method isn't always too heavy to run? For example in the code above with the method, would it be possible to say to only look for two variables if the four other are equal to 500 (myVariable1 and myVariable2 instead of all the variables even if the whole method is defined for the rest?).
Thanks in advance !
Upvotes: 0
Views: 103
Reputation: 14274
You want this --
void myVariableMethod( int... catIds ) {
for( int catId : catIds ) {
Log.d( "Output", "catId: " + catId );
}
}
Adding the ellipsis after the type in a method declaration will allow you to pass any number of the same type of arguments to the method. catIds
is then actually an int[]
array within the method body.
You can call this method as follows:
myVariableMethod( 1, 2, 3, 4 );
myVariableMethod( 1 );
myVariableMethod( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 );
myVariableMethod( 1, 2, 3, 4, 100, 101, 102 );
A more elegant solution to your problem would be to use a List<Integer>
, or Set<Integer>
and add those ids as you evaluate your buttons. Lastly, you would pass the list to the method and evaluate it properly.
My gut says that you can improve your database design to avoid the WHERE ... IN
construct entirely, but that's difficult to determine without seeing the ERD.
Upvotes: 2
Reputation: 87
surely you can use the define you method with a Vararg as :
public Cursor findPoiInTable(int ... myVariable)
{
//construct String from myVariable array
StringBuffer sb = new StringBuffer("(");
for (int i : myVariable)
{
sb.append(",?");
}
sb.append(")");
String inInterval = sb.toString();
String where = COL_CAT1 + " IN " + inInterval + " OR " + COL_CAT2
+ " IN " + inInterval + " OR " + COL_CAT3 + " IN " + inInterval;
String[] whereArgs = new String[3 * myVariable.length];
int j = 0
for (int i = 0; i < 3; i++)
{
whereArgs[6 * i + j] = Integer.toString(myVariable[j++]);
whereArgs[6 * i + j] = Integer.toString(myVariable[j++]);
whereArgs[6 * i + j] = Integer.toString(myVariable[j++]);
whereArgs[6 * i + j] = Integer.toString(myVariable[j++]);
whereArgs[6 * i + j] = Integer.toString(myVariable[j++]);
whereArgs[6 * i + j] = Integer.toString(myVariable[j++]);
}
Cursor c = myDatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID,
COL_NAME, COL_STREET}, where, whereArgs, null, null, null);
return c;
}
Upvotes: 1