android - Asynctask define a Constructor -
in app, trying build asynctask should, in background each row sql database , write csv file.
in activity have button onclicklistener executes asycntaskrunner so:
//this task run in background , transmit data csv asynctaskrunner runner = new asynctaskrunner(); runner.execute();
then in asynctaskrunner class have following:
public class asynctaskrunner extends asynctask { protected string doinbackground(string... params) { // create new csv file based tid of newest test log.e(csv, "async task runner string started"); return null; } @override protected object doinbackground(object... params) { log.e(csv, "async task runner object started"); filenamecsv = mcreatenewcsvfile(); log.d(csv, "the new csv file " + filenamecsv); return null; } public string mcreatenewcsvfile() { sqldatabase nexttest = new sqldatabase(this); nexttest.open(); string csvfilename = sqldatabase.getrunningtests(); nexttest.close(); return csvfilename; } }
on execute, asynctask should start going sqldatabase class , running getrunningtests select database , return string used filename.
my error comes constructor or "this". quick fix recommends create new constructor sqldatabase(asynctaskrunner) in class sqldatabase, there constructor there of
public sqldatabase(context c) { thiscontext = c; }
i need asynctaskrunner able open database, other classes using database while writing database. asynctaskrunner reading form database. can not open it.
how should build constructor function or else recommend do?
hope solve problem.
public class asynctaskrunner extends asynctask { context mcontext; public asynctaskrunner(context _mcontext) { mcontext = _mcontext; } protected string doinbackground(string... params) { // create new csv file based tid of newest test log.e(csv, "async task runner string started"); return null; } @override protected object doinbackground(object... params) { log.e(csv, "async task runner object started"); filenamecsv = mcreatenewcsvfile(); log.d(csv, "the new csv file " + filenamecsv); return null; } public string mcreatenewcsvfile() { sqldatabase nexttest = new sqldatabase(mcontext); nexttest.open(); string csvfilename = sqldatabase.getrunningtests(); nexttest.close(); return csvfilename; } }
and while calling, use
asynctaskrunner runner = new asynctaskrunner(this); runner.execute();
this => activity context
Comments
Post a Comment