android - Why will my cursor only iterate 1 column at a time through my SQLite database even if I am selecting 2 columns? -


i have set query , cursor. problem cursor seems moving 1 column @ time. table set first column description string, second column grade value , third column weight value.

this query:

string[] columns = {calc_db.column_grade_value, calc_db.column_weight_value};         cursor cursor = openclass.getbd().query(calc_db.table_name, columns, null, null, null, null, null); 

i trying retrieve grade , weight value can put them in list.

cursor.movetofirst();         while (cursor.movetonext()){             double dbgrade = cursor.getdouble(cursor.getcolumnindex(calc_db.column_grade_value));             //cursor.movetonext();             double dbweight = cursor.getdouble(cursor.getcolumnindex(calc_db.column_weight_value));             gradeweightlist.add(dbgrade);             gradeweightlist.add(dbweight); 

my problem cursor going through 1 column @ time each loop iteration. going through 3 columns though selecting second 2 (grade , weight).

example:

if input : description, 0.7, 0.5 (row 1) decript, 0.6, 0.4 (row 2)

i receive:

loop 1: grade = 0.7, weight = 0.0

loop 2: grade = 0.7, weight = 0.5

loop 3: grade = 0.7, weight = 0.5

loop 4: grade = 0.6, weight = 0.5

loop 5: grade = 0.6, weight = 0.4

what want

loop 1: grade = 0.7, weight = 0.5

loop 2: grade = 0.6, weight = 0.4

i see bugs in code. first bug:

cursor.movetofirst(); while (cursor.movetonext()) {      ... } 

this code skip first row. moving first row movetofirst, moving second row movetonext when while loop condition evaluated. in general, prefer iterate on cursor using for loop so:

for (cursor.movetofirst(); !cursor.isafterlast(); cursor.movetonext()) {     ... } 

this code work no matter position cursor @ before for loop started.

second bug:

double dbgrade = cursor.getdouble(...); double dbweight = cursor.getdouble(...); gradeweightlist.add(dbgrade); gradeweightlist.add(dbweight); 

notice adding both doubles same list (which call gradeweightlist). presume have different lists grades , weights? while change line incorrect, might want consider making class encapsulates grade data, e.g.

public class gradedata {     double grade;     double gradeweight;     /* constructors , methods omitted */ } 

... , use single list of type of object. each row, make new instance of class, set properties, , add list. way aren't trying manage disparate lists , keep them in sync, if decide grade data needs more attributes (which in current implementation mean adding third list).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -