Michelle
Michelle

Reputation: 852

How put cursor (android) to array 2 dimension?

I have this :

String [][] population = new String[indexOfChromosomes][indexOfGens];

for(int chromosomesNumber = 0; chromosomesNumber <= indexOfChromosomes ; chromosomesNumber++){
  for(int gensNumber = 0; gensNumber <= indexOfGens ; gensNumber++){
    Cursor r = db.rawQuery("SELECT _id, key_foodstuff, key_calorie, key_carbohydrate, key_fat, 
      key_protein FROM (food INNER JOIN categories ON food.key_nocategory = 
      categories.nocategories) WHERE key_type='primary' 
      AND _id!=164 ORDER BY RANDOM() LIMIT 1", null);       

      if(r.moveToFirst()){              
       population[indexOfChromosomes][indexOfGens] = r.getString(0);
      }
   }
}

I got error when I run for this code : population[indexOfChromosomes][indexOfGens] = r.getString(0);

I wanna ask, how I put that cursor r on array 2 dimension ? Thx

Upvotes: 0

Views: 669

Answers (3)

Fletcher Johns
Fletcher Johns

Reputation: 1274

Your approach confuses me. I don't know a lot about SQL, but it looks like you're repeatedly querying your database for a single row at random. I've read that this is a costly operation. I think you're better off getting the full data set once, at the beginning, and iterating over that.

How do indexOfChromosomes and indexOfGens relate to the database with columns "foodstuff" etc? I'm not seeing a connection...

This is modified from my own method that works well in Android. If the rawQuery works, then the whole lot should:

// The order of this array will affect final array
// I would also use this array in the query itself
String[] columnNames = {
        "_id", "key_foodstuff", "key_calorie", "key_carbohydrate", "key_fat", "key_protein"
};
Cursor r = db.rawQuery("SELECT _id, key_foodstuff, key_calorie,
        key_carbohydrate, key_fat, key_protein FROM (food INNER JOIN
        categories ON food.key_nocategory = categories.nocategories)
        WHERE key_type='primary' AND _id!=164", null);

int rowCount = r.getCount();
int columnCount = columnNames.length;
// create the required array with appropriate dimensions
String[][] array = new String[rowCount][columnCount];
// begin iterating over the cursor
if (r.moveToFirst()) {
    int row = 0;
    do {
        // for each row of the cursor, get the values from all
        // of the columns
        for (int column = 0; column < columnCount; column++) {
            array[row][column] = cursor.getString(cursor.
                    getColumnIndex(columnNames[column]));
        }
        row++;
    } while (r.moveToNext());
}

Upvotes: 0

Vladimir
Vladimir

Reputation: 9743

it looks like error is here :

String [][] population = new String[indexOfChromosomes][indexOfGens];

for(int chromosomesNumber = 0; chromosomesNumber <= indexOfChromosomes ; chromosomesNumber++){
    for(int gensNumber = 0; gensNumber <= indexOfGens ; gensNumber++){

size of array is indexOfChromosomes x indexOfGens, so it's indexes are 0..indexOfChromosomes-1, and 0..indexOfGens-1, while you are trying to access index indexOfChromosomes and indexOfGens.

It should be

for(int chromosomesNumber = 0; chromosomesNumber < indexOfChromosomes ; chromosomesNumber++){
    for(int gensNumber = 0; gensNumber < indexOfGens ; gensNumber++){

Upvotes: 1

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Based on how you've written your code, it looks like inside your for loop

 population[indexOfChromosomes][indexOfGens]

should be

population[chromosomesNumber][gensNumber]

Upvotes: 0

Related Questions