I find this code quite ugly, but in fact, I can't find a finer way of achieving the goal.
private static final String TABLE_NAME = "table_name";
private static final String[] allNeededColumns = {"col_id","col_one","col_two"};
public int[] getItemIds(int category_id) {
ArrayList<Integer> ids = new ArrayList<Integer>();
Cursor c = getDatabase().query(TABLE_NAME, allNeededColumns, CATEGORY_ID + "=" + category_id, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast()){
ids.add(Integer.valueOf(c.getInt(2)));
c.moveToNext();
}
return convertIntegers(ids);
}
public static int[] convertIntegers(ArrayList<Integer> integers)
{
int[] ret = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
}
return ret;
}
The purpose is to get out of table int array, where IDs are item IDs from category by
category_id`.
The problem is, I need some kind of expandable collection to add DB result ID to, and return primitives array, and I find it prettier than creating new int[]
variable for each iteration on Cursor
.
Can you think of a prettier solution?