android - Reading integers from SQLite database returns 0? -
in android application, have sqlite database stores students grade information. able read in category of grade correctly reason when try read in grade rescurs.getint(gradeindex) returns 0 every time though grade value in database not 0. appreciated see nothing wrong code
gradedbopenhelper.java
package bcs421.jorgeramirez.hwk.gradeapp.adv; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; public class gradedbopenhelper extends sqliteopenhelper { public static final string database_name = "gradedata.db"; public static final string database_table = "gradedata"; public static final int database_version = 1; public static final string key_id = "_id"; public static final string column_cat = "category"; public static final string column_num = "itemnumber"; public static final string column_desc = "description"; public static final string column_grd = "grade"; public static final string column_date = "date"; static final string database_create = "create table " + database_table + "(" + key_id + " integer primary key autoincrement, " + column_cat + " varchar(20), " + column_num + " integer, " + column_desc + " varchar(255), " + column_grd + " integer, " + column_date + " varchar(255));"; private static final string student_select = "select * " + database_table + ";"; static final string grade_insert_1 = "insert " + database_table + " values (null, 'homework', 1, 'assigment 1 - schedule app', 100, '02/10/2015');"; static final string grade_insert_2 = "insert " + database_table + " values (null, 'quiz', 1, 'quiz - syllabus', 100, '02/10/2015');"; static final string grade_insert_3 = "insert " + database_table + " values (null, 'lab', 1, 'lab ch1 - hello yankees', 100, '01/28/2015');"; static final string grade_insert_4 = "insert " + database_table + " values (null, 'lab', 2, 'lab - hello world', 100, '02/04/2015');"; static final string grade_insert_5 = "insert " + database_table + " values (null, 'lab', 3, 'lab - manifest , different screens', 0, '02/09/2015');"; public gradedbopenhelper(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase db) { db.execsql(database_create); db.execsql(grade_insert_1); db.execsql(grade_insert_2); db.execsql(grade_insert_3); db.execsql(grade_insert_4); db.execsql(grade_insert_5); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } }
mainactivity.java
package bcs421.jorgeramirez.hwk.gradeapp.adv; import android.app.activity; import android.content.contentresolver; import android.content.intent; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string[] projection = { gradedbopenhelper.key_id, gradedbopenhelper.column_cat, gradedbopenhelper.column_num, gradedbopenhelper.column_desc, gradedbopenhelper.column_grd, gradedbopenhelper.column_date }; // content resolver contentresolver cr = getcontentresolver(); // query contentprovider using contentresolver uri targeturi = uri.parse("content://bcs421.jorgeramirez.hwk.gradeapp"); cursor resultcursor = cr.query(targeturi, projection, null, null, null); textview numgrade = (textview)findviewbyid(r.id.numgrade); string numg = double.tostring(calcaverage(resultcursor)); numgrade.settext(numg); button viewgradelist = (button)findviewbyid(r.id.viewgradesbutton); viewgradelist.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { intent intent = new intent(mainactivity.this, gradelistactivity.class); startactivity(intent); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public double calcaverage (cursor rescurs) { int hwksum = 0, examsum = 0, labsum = 0, quizsum = 0; int hwkcnt = 0, examcnt = 0, labcnt = 0, quizcnt = 0; double hwkavg = 0, examavg = 0, labavg = 0, quizavg = 0; double hwkportion, examportion, labportion, quizportion; double totaverage; int categoryindex = rescurs.getcolumnindex(gradedbopenhelper.column_cat); int gradeindex = rescurs.getcolumnindex(gradedbopenhelper.column_grd); string category; int gd; while (rescurs.movetonext()) { category = rescurs.getstring(categoryindex); gd = rescurs.getint(gradeindex); switch (category) { case "homework": hwksum = hwksum + gd; ++hwkcnt; break; case "exam": examsum = examsum + gd; ++examcnt; break; case "lab": labsum = labsum + gd; ++labcnt; break; case "quiz": quizsum = quizsum + gd; ++quizcnt; break; default: break; } } if (hwkcnt != 0) { hwkavg = hwksum/hwkcnt; } if(examcnt != 0) { examavg = examsum/examcnt; } if(labcnt != 0) { labavg = labsum/labcnt; } if(quizcnt != 0) { quizavg = quizsum/quizcnt; } hwkportion = hwkavg * .35; examportion = examavg * .50; labportion = labavg * .05; quizportion = quizavg * .10; totaverage = hwkportion + examportion + labportion + quizportion; return totaverage; } }
Comments
Post a Comment