java - Getting values from multi-dimensional array too slow -
good day, i've been working on update in android project, , came across issue. have read questions sqlite database i've done loading multi-dimensional array shown below in database helper class:
public string getsome(int s,int t, string table_name){ string selectquery = "select * " + table_name; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(selectquery, null); int rows = cursor.getcount(); int num=0; int col = 0; string[][] base = new string[rows][13]; if (cursor.movetofirst()) { { (col=0;col<13;++col ){ base[num][col] = (cursor.getstring(col));} ++num; } while (cursor.movetonext()); return base[s][t]; } return null; }
with done, read questions such in question class:
public void database_calls(){ setcoursetag(coursetag); mydbhelper = new databasehelper(getactivity()); try { mydbhelper.createdatabase(); } catch (ioexception ioe) { throw new error("unable create database"); } try { mydbhelper.opendatabase(); }catch(sqlexception sqle){ throw sqle; } string no= mydbhelper.getsome(ques,0, getcoursetag()); string qu = mydbhelper.getsome(ques,1, getcoursetag()); string = mydbhelper.getsome(ques,2, getcoursetag()); string b = mydbhelper.getsome(ques,3, getcoursetag()); string c = mydbhelper.getsome(ques,4, getcoursetag()); string d = mydbhelper.getsome(ques,5, getcoursetag()); ans = mydbhelper.getsome(ques,6, getcoursetag()); img = integer.parseint(mydbhelper.getsome(ques,8, getcoursetag())); exp = mydbhelper.getsome(ques,9, getcoursetag()); year = mydbhelper.getsome(ques,10, getcoursetag()); questionimage = mydbhelper.getsome(ques,11, getcoursetag()); length = mydbhelper.getmax(getcoursetag()); }
so recently, tried use year
column, (i.e, column 10) qualify questions chosen each quiz session, user may able select questions year, he/she wants attempt. in order this, used loop @ beginning of activity filter out required year past questions. transferred indices of each question set, bal
, iterated , on..
public void countyearquestions(){ for(int y = 0; y < length; ++y){ //year = mydbhelper.getsome(y,10, getcoursetag()); if (selectedyear.equals(mydbhelper.getsome(y,10, getcoursetag()))) bal.add(y); } }
here, length size of entire question database, course, (indicated getcoursetag()). code works quite alright. takes whole 8-9secs!! activity load. on how reduce loading time appreciated.
the way you're doing pretty efficient in slowing down (and helper helps lot):
string no= mydbhelper.getsome(ques,0, getcoursetag());
in each such line execute query, create 2d array holding whole table , throw away. and fail close cursor
.
so need 12 values table , read instead whole table 12 times.
Comments
Post a Comment