Initialize database using kotlin in AsyncTask












1















In my doInBackground I declare and Initialize db, I got an error which say type mismatch. What should I put instead of putting this?



var dbHelper: MyDBHelper? = null
dbHelper = MyDBHelper(this)


What should I put, There say Required: Context!



And this is my async task code, That Problem at dbHelper = MyDBHelper(this).



private class UpgradeDB(textView: TextView?) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView

override fun onPreExecute() {
innerTextView!!.visibility = View.VISIBLE
}

override fun doInBackground(vararg params: String): String? {
val filename = "eBOSSInv_Upgrade.sql"
val sdcard = Environment.getExternalStorageDirectory()
val file = File(sdcard, filename)

if (!file.exists()) isCancelled

try {
var dbHelper: MyDBHelper? = null

dbHelper = MyDBHelper(this)

dbHelper!!.writableDatabase.use { db ->
var intTotalLine = 0
var intLine = 1
BufferedReader(FileReader(file)).useLines { _ -> intTotalLine++ }
BufferedReader(FileReader(file)).use { r ->
r.lineSequence().forEach {
if (it.isNotEmpty()) {
db!!.execSQL(it)
publishProgress(String.format("Updating %s/%s records", intLine, intTotalLine))
intLine++
}
}
}
}
} catch (e: Exception) {

}
return null
}

override fun onProgressUpdate(vararg text: String) {
innerTextView!!.text = text[0]
}

override fun onPostExecute(result: String?) {
innerTextView!!.text = ""
}

override fun onCancelled() {

}
}









share|improve this question

























  • "MainActivity@this" like this

    – Mustafa Bohra
    Nov 26 '18 at 7:11













  • Please show a Minimal, Complete, and Verifiable example of all relevant code. What is this here? Where is the AsyncTask located? Do you really need to declare the database inside the AsyncTask?

    – cricket_007
    Nov 26 '18 at 7:12











  • Sorry this may sound a bit blunt, but maybe you should focus on understanding Android before you go about working with Kotlin also, which looks like you haven't worked with much (looking at the code). You have quite a lot of issues in the Android code, that are not specific to Kotlin and if you don't know what this is in this case, then I guess you haven't even written the code in the first place. I think you're digging in too deep into areas that you don't fully understand yet. Depending on what this is here it looks like you could end up with a memory leak for instance.

    – Darwind
    Nov 26 '18 at 9:16






  • 1





    problem fixed. Thank you guys~

    – low zaii
    Nov 26 '18 at 9:19











  • Noted, But Those code was done by previous employee, It caused me a lot of problems. @Darwind

    – low zaii
    Nov 26 '18 at 9:24
















1















In my doInBackground I declare and Initialize db, I got an error which say type mismatch. What should I put instead of putting this?



var dbHelper: MyDBHelper? = null
dbHelper = MyDBHelper(this)


What should I put, There say Required: Context!



And this is my async task code, That Problem at dbHelper = MyDBHelper(this).



private class UpgradeDB(textView: TextView?) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView

override fun onPreExecute() {
innerTextView!!.visibility = View.VISIBLE
}

override fun doInBackground(vararg params: String): String? {
val filename = "eBOSSInv_Upgrade.sql"
val sdcard = Environment.getExternalStorageDirectory()
val file = File(sdcard, filename)

if (!file.exists()) isCancelled

try {
var dbHelper: MyDBHelper? = null

dbHelper = MyDBHelper(this)

dbHelper!!.writableDatabase.use { db ->
var intTotalLine = 0
var intLine = 1
BufferedReader(FileReader(file)).useLines { _ -> intTotalLine++ }
BufferedReader(FileReader(file)).use { r ->
r.lineSequence().forEach {
if (it.isNotEmpty()) {
db!!.execSQL(it)
publishProgress(String.format("Updating %s/%s records", intLine, intTotalLine))
intLine++
}
}
}
}
} catch (e: Exception) {

}
return null
}

override fun onProgressUpdate(vararg text: String) {
innerTextView!!.text = text[0]
}

override fun onPostExecute(result: String?) {
innerTextView!!.text = ""
}

override fun onCancelled() {

}
}









share|improve this question

























  • "MainActivity@this" like this

    – Mustafa Bohra
    Nov 26 '18 at 7:11













  • Please show a Minimal, Complete, and Verifiable example of all relevant code. What is this here? Where is the AsyncTask located? Do you really need to declare the database inside the AsyncTask?

    – cricket_007
    Nov 26 '18 at 7:12











  • Sorry this may sound a bit blunt, but maybe you should focus on understanding Android before you go about working with Kotlin also, which looks like you haven't worked with much (looking at the code). You have quite a lot of issues in the Android code, that are not specific to Kotlin and if you don't know what this is in this case, then I guess you haven't even written the code in the first place. I think you're digging in too deep into areas that you don't fully understand yet. Depending on what this is here it looks like you could end up with a memory leak for instance.

    – Darwind
    Nov 26 '18 at 9:16






  • 1





    problem fixed. Thank you guys~

    – low zaii
    Nov 26 '18 at 9:19











  • Noted, But Those code was done by previous employee, It caused me a lot of problems. @Darwind

    – low zaii
    Nov 26 '18 at 9:24














1












1








1








In my doInBackground I declare and Initialize db, I got an error which say type mismatch. What should I put instead of putting this?



var dbHelper: MyDBHelper? = null
dbHelper = MyDBHelper(this)


What should I put, There say Required: Context!



And this is my async task code, That Problem at dbHelper = MyDBHelper(this).



private class UpgradeDB(textView: TextView?) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView

override fun onPreExecute() {
innerTextView!!.visibility = View.VISIBLE
}

override fun doInBackground(vararg params: String): String? {
val filename = "eBOSSInv_Upgrade.sql"
val sdcard = Environment.getExternalStorageDirectory()
val file = File(sdcard, filename)

if (!file.exists()) isCancelled

try {
var dbHelper: MyDBHelper? = null

dbHelper = MyDBHelper(this)

dbHelper!!.writableDatabase.use { db ->
var intTotalLine = 0
var intLine = 1
BufferedReader(FileReader(file)).useLines { _ -> intTotalLine++ }
BufferedReader(FileReader(file)).use { r ->
r.lineSequence().forEach {
if (it.isNotEmpty()) {
db!!.execSQL(it)
publishProgress(String.format("Updating %s/%s records", intLine, intTotalLine))
intLine++
}
}
}
}
} catch (e: Exception) {

}
return null
}

override fun onProgressUpdate(vararg text: String) {
innerTextView!!.text = text[0]
}

override fun onPostExecute(result: String?) {
innerTextView!!.text = ""
}

override fun onCancelled() {

}
}









share|improve this question
















In my doInBackground I declare and Initialize db, I got an error which say type mismatch. What should I put instead of putting this?



var dbHelper: MyDBHelper? = null
dbHelper = MyDBHelper(this)


What should I put, There say Required: Context!



And this is my async task code, That Problem at dbHelper = MyDBHelper(this).



private class UpgradeDB(textView: TextView?) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView

override fun onPreExecute() {
innerTextView!!.visibility = View.VISIBLE
}

override fun doInBackground(vararg params: String): String? {
val filename = "eBOSSInv_Upgrade.sql"
val sdcard = Environment.getExternalStorageDirectory()
val file = File(sdcard, filename)

if (!file.exists()) isCancelled

try {
var dbHelper: MyDBHelper? = null

dbHelper = MyDBHelper(this)

dbHelper!!.writableDatabase.use { db ->
var intTotalLine = 0
var intLine = 1
BufferedReader(FileReader(file)).useLines { _ -> intTotalLine++ }
BufferedReader(FileReader(file)).use { r ->
r.lineSequence().forEach {
if (it.isNotEmpty()) {
db!!.execSQL(it)
publishProgress(String.format("Updating %s/%s records", intLine, intTotalLine))
intLine++
}
}
}
}
} catch (e: Exception) {

}
return null
}

override fun onProgressUpdate(vararg text: String) {
innerTextView!!.text = text[0]
}

override fun onPostExecute(result: String?) {
innerTextView!!.text = ""
}

override fun onCancelled() {

}
}






android kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 8:23







low zaii

















asked Nov 26 '18 at 7:09









low zaiilow zaii

83




83













  • "MainActivity@this" like this

    – Mustafa Bohra
    Nov 26 '18 at 7:11













  • Please show a Minimal, Complete, and Verifiable example of all relevant code. What is this here? Where is the AsyncTask located? Do you really need to declare the database inside the AsyncTask?

    – cricket_007
    Nov 26 '18 at 7:12











  • Sorry this may sound a bit blunt, but maybe you should focus on understanding Android before you go about working with Kotlin also, which looks like you haven't worked with much (looking at the code). You have quite a lot of issues in the Android code, that are not specific to Kotlin and if you don't know what this is in this case, then I guess you haven't even written the code in the first place. I think you're digging in too deep into areas that you don't fully understand yet. Depending on what this is here it looks like you could end up with a memory leak for instance.

    – Darwind
    Nov 26 '18 at 9:16






  • 1





    problem fixed. Thank you guys~

    – low zaii
    Nov 26 '18 at 9:19











  • Noted, But Those code was done by previous employee, It caused me a lot of problems. @Darwind

    – low zaii
    Nov 26 '18 at 9:24



















  • "MainActivity@this" like this

    – Mustafa Bohra
    Nov 26 '18 at 7:11













  • Please show a Minimal, Complete, and Verifiable example of all relevant code. What is this here? Where is the AsyncTask located? Do you really need to declare the database inside the AsyncTask?

    – cricket_007
    Nov 26 '18 at 7:12











  • Sorry this may sound a bit blunt, but maybe you should focus on understanding Android before you go about working with Kotlin also, which looks like you haven't worked with much (looking at the code). You have quite a lot of issues in the Android code, that are not specific to Kotlin and if you don't know what this is in this case, then I guess you haven't even written the code in the first place. I think you're digging in too deep into areas that you don't fully understand yet. Depending on what this is here it looks like you could end up with a memory leak for instance.

    – Darwind
    Nov 26 '18 at 9:16






  • 1





    problem fixed. Thank you guys~

    – low zaii
    Nov 26 '18 at 9:19











  • Noted, But Those code was done by previous employee, It caused me a lot of problems. @Darwind

    – low zaii
    Nov 26 '18 at 9:24

















"MainActivity@this" like this

– Mustafa Bohra
Nov 26 '18 at 7:11







"MainActivity@this" like this

– Mustafa Bohra
Nov 26 '18 at 7:11















Please show a Minimal, Complete, and Verifiable example of all relevant code. What is this here? Where is the AsyncTask located? Do you really need to declare the database inside the AsyncTask?

– cricket_007
Nov 26 '18 at 7:12





Please show a Minimal, Complete, and Verifiable example of all relevant code. What is this here? Where is the AsyncTask located? Do you really need to declare the database inside the AsyncTask?

– cricket_007
Nov 26 '18 at 7:12













Sorry this may sound a bit blunt, but maybe you should focus on understanding Android before you go about working with Kotlin also, which looks like you haven't worked with much (looking at the code). You have quite a lot of issues in the Android code, that are not specific to Kotlin and if you don't know what this is in this case, then I guess you haven't even written the code in the first place. I think you're digging in too deep into areas that you don't fully understand yet. Depending on what this is here it looks like you could end up with a memory leak for instance.

– Darwind
Nov 26 '18 at 9:16





Sorry this may sound a bit blunt, but maybe you should focus on understanding Android before you go about working with Kotlin also, which looks like you haven't worked with much (looking at the code). You have quite a lot of issues in the Android code, that are not specific to Kotlin and if you don't know what this is in this case, then I guess you haven't even written the code in the first place. I think you're digging in too deep into areas that you don't fully understand yet. Depending on what this is here it looks like you could end up with a memory leak for instance.

– Darwind
Nov 26 '18 at 9:16




1




1





problem fixed. Thank you guys~

– low zaii
Nov 26 '18 at 9:19





problem fixed. Thank you guys~

– low zaii
Nov 26 '18 at 9:19













Noted, But Those code was done by previous employee, It caused me a lot of problems. @Darwind

– low zaii
Nov 26 '18 at 9:24





Noted, But Those code was done by previous employee, It caused me a lot of problems. @Darwind

– low zaii
Nov 26 '18 at 9:24












2 Answers
2






active

oldest

votes


















0














You need to pass context into Async Task.



Example:



llUpdate.setOnClickListener { 
UpgradeDB(txtUpdate!!, getApplication()).execute("", "", "")
}


Then in UpgradeDB, initialize the context.



private class UpgradeDB(textView: TextView?, context: Context) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView
var mContext:Context? = context // initialize context
}


And finally



dbHelper = MyDBHelper(mContext)





share|improve this answer
























  • It works,Thank you very much.

    – low zaii
    Nov 26 '18 at 9:12











  • @lowzaii my pleasure

    – John Joe
    Nov 26 '18 at 9:13













  • While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

    – Darwind
    Nov 26 '18 at 9:18



















0














It depends where you try to create this object. Unless you do it in a class that extends Context (like Activity) it is not supposed to work. You can try such construct:



There are two ways you gan go:



The simpler way: Declare your AsyncTask as inner class inside tour activity and reference context using label. To declare class as inner you need to place it inside another class and add inner keyowrd:



class A {

inner class B

}


Now you can reference context from inside your async task like that:



dbHelper = DbHelper([ActivityName]@this)


Nevertheless, this is easy way to cause a memory leak, you can read more here: Android AsyncTask memory leaks



The better way is to extract AsyncTask to another class and pass an application context to it, instead of Activity.






share|improve this answer


























  • I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

    – low zaii
    Nov 26 '18 at 7:24











  • @lowzaii could you please show your activity and async task code?

    – Michał Powłoka
    Nov 26 '18 at 7:52











  • can you open a chat room ? @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:03











  • @lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

    – Michał Powłoka
    Nov 26 '18 at 8:15











  • Okay, I've updated my code. @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476189%2finitialize-database-using-kotlin-in-asynctask%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to pass context into Async Task.



Example:



llUpdate.setOnClickListener { 
UpgradeDB(txtUpdate!!, getApplication()).execute("", "", "")
}


Then in UpgradeDB, initialize the context.



private class UpgradeDB(textView: TextView?, context: Context) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView
var mContext:Context? = context // initialize context
}


And finally



dbHelper = MyDBHelper(mContext)





share|improve this answer
























  • It works,Thank you very much.

    – low zaii
    Nov 26 '18 at 9:12











  • @lowzaii my pleasure

    – John Joe
    Nov 26 '18 at 9:13













  • While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

    – Darwind
    Nov 26 '18 at 9:18
















0














You need to pass context into Async Task.



Example:



llUpdate.setOnClickListener { 
UpgradeDB(txtUpdate!!, getApplication()).execute("", "", "")
}


Then in UpgradeDB, initialize the context.



private class UpgradeDB(textView: TextView?, context: Context) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView
var mContext:Context? = context // initialize context
}


And finally



dbHelper = MyDBHelper(mContext)





share|improve this answer
























  • It works,Thank you very much.

    – low zaii
    Nov 26 '18 at 9:12











  • @lowzaii my pleasure

    – John Joe
    Nov 26 '18 at 9:13













  • While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

    – Darwind
    Nov 26 '18 at 9:18














0












0








0







You need to pass context into Async Task.



Example:



llUpdate.setOnClickListener { 
UpgradeDB(txtUpdate!!, getApplication()).execute("", "", "")
}


Then in UpgradeDB, initialize the context.



private class UpgradeDB(textView: TextView?, context: Context) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView
var mContext:Context? = context // initialize context
}


And finally



dbHelper = MyDBHelper(mContext)





share|improve this answer













You need to pass context into Async Task.



Example:



llUpdate.setOnClickListener { 
UpgradeDB(txtUpdate!!, getApplication()).execute("", "", "")
}


Then in UpgradeDB, initialize the context.



private class UpgradeDB(textView: TextView?, context: Context) : AsyncTask<String, String, String>() {
var innerTextView: TextView? = textView
var mContext:Context? = context // initialize context
}


And finally



dbHelper = MyDBHelper(mContext)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 9:07









John JoeJohn Joe

4,71822260




4,71822260













  • It works,Thank you very much.

    – low zaii
    Nov 26 '18 at 9:12











  • @lowzaii my pleasure

    – John Joe
    Nov 26 '18 at 9:13













  • While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

    – Darwind
    Nov 26 '18 at 9:18



















  • It works,Thank you very much.

    – low zaii
    Nov 26 '18 at 9:12











  • @lowzaii my pleasure

    – John Joe
    Nov 26 '18 at 9:13













  • While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

    – Darwind
    Nov 26 '18 at 9:18

















It works,Thank you very much.

– low zaii
Nov 26 '18 at 9:12





It works,Thank you very much.

– low zaii
Nov 26 '18 at 9:12













@lowzaii my pleasure

– John Joe
Nov 26 '18 at 9:13







@lowzaii my pleasure

– John Joe
Nov 26 '18 at 9:13















While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

– Darwind
Nov 26 '18 at 9:18





While this will solve the problem I think you should mention that this might cause issues, as you leave a Context hanging around in an AsyncTask that potentially doesn't finish before the Context is gone. What issues might this cause?

– Darwind
Nov 26 '18 at 9:18













0














It depends where you try to create this object. Unless you do it in a class that extends Context (like Activity) it is not supposed to work. You can try such construct:



There are two ways you gan go:



The simpler way: Declare your AsyncTask as inner class inside tour activity and reference context using label. To declare class as inner you need to place it inside another class and add inner keyowrd:



class A {

inner class B

}


Now you can reference context from inside your async task like that:



dbHelper = DbHelper([ActivityName]@this)


Nevertheless, this is easy way to cause a memory leak, you can read more here: Android AsyncTask memory leaks



The better way is to extract AsyncTask to another class and pass an application context to it, instead of Activity.






share|improve this answer


























  • I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

    – low zaii
    Nov 26 '18 at 7:24











  • @lowzaii could you please show your activity and async task code?

    – Michał Powłoka
    Nov 26 '18 at 7:52











  • can you open a chat room ? @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:03











  • @lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

    – Michał Powłoka
    Nov 26 '18 at 8:15











  • Okay, I've updated my code. @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:24
















0














It depends where you try to create this object. Unless you do it in a class that extends Context (like Activity) it is not supposed to work. You can try such construct:



There are two ways you gan go:



The simpler way: Declare your AsyncTask as inner class inside tour activity and reference context using label. To declare class as inner you need to place it inside another class and add inner keyowrd:



class A {

inner class B

}


Now you can reference context from inside your async task like that:



dbHelper = DbHelper([ActivityName]@this)


Nevertheless, this is easy way to cause a memory leak, you can read more here: Android AsyncTask memory leaks



The better way is to extract AsyncTask to another class and pass an application context to it, instead of Activity.






share|improve this answer


























  • I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

    – low zaii
    Nov 26 '18 at 7:24











  • @lowzaii could you please show your activity and async task code?

    – Michał Powłoka
    Nov 26 '18 at 7:52











  • can you open a chat room ? @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:03











  • @lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

    – Michał Powłoka
    Nov 26 '18 at 8:15











  • Okay, I've updated my code. @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:24














0












0








0







It depends where you try to create this object. Unless you do it in a class that extends Context (like Activity) it is not supposed to work. You can try such construct:



There are two ways you gan go:



The simpler way: Declare your AsyncTask as inner class inside tour activity and reference context using label. To declare class as inner you need to place it inside another class and add inner keyowrd:



class A {

inner class B

}


Now you can reference context from inside your async task like that:



dbHelper = DbHelper([ActivityName]@this)


Nevertheless, this is easy way to cause a memory leak, you can read more here: Android AsyncTask memory leaks



The better way is to extract AsyncTask to another class and pass an application context to it, instead of Activity.






share|improve this answer















It depends where you try to create this object. Unless you do it in a class that extends Context (like Activity) it is not supposed to work. You can try such construct:



There are two ways you gan go:



The simpler way: Declare your AsyncTask as inner class inside tour activity and reference context using label. To declare class as inner you need to place it inside another class and add inner keyowrd:



class A {

inner class B

}


Now you can reference context from inside your async task like that:



dbHelper = DbHelper([ActivityName]@this)


Nevertheless, this is easy way to cause a memory leak, you can read more here: Android AsyncTask memory leaks



The better way is to extract AsyncTask to another class and pass an application context to it, instead of Activity.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 7:22

























answered Nov 26 '18 at 7:13









Michał PowłokaMichał Powłoka

161113




161113













  • I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

    – low zaii
    Nov 26 '18 at 7:24











  • @lowzaii could you please show your activity and async task code?

    – Michał Powłoka
    Nov 26 '18 at 7:52











  • can you open a chat room ? @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:03











  • @lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

    – Michał Powłoka
    Nov 26 '18 at 8:15











  • Okay, I've updated my code. @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:24



















  • I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

    – low zaii
    Nov 26 '18 at 7:24











  • @lowzaii could you please show your activity and async task code?

    – Michał Powłoka
    Nov 26 '18 at 7:52











  • can you open a chat room ? @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:03











  • @lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

    – Michał Powłoka
    Nov 26 '18 at 8:15











  • Okay, I've updated my code. @Michal Powloka

    – low zaii
    Nov 26 '18 at 8:24

















I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

– low zaii
Nov 26 '18 at 7:24





I'm using this dbHelper = MyBDHelper([MainDBAdminActivity]@this), It's doesn't work.

– low zaii
Nov 26 '18 at 7:24













@lowzaii could you please show your activity and async task code?

– Michał Powłoka
Nov 26 '18 at 7:52





@lowzaii could you please show your activity and async task code?

– Michał Powłoka
Nov 26 '18 at 7:52













can you open a chat room ? @Michal Powloka

– low zaii
Nov 26 '18 at 8:03





can you open a chat room ? @Michal Powloka

– low zaii
Nov 26 '18 at 8:03













@lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

– Michał Powłoka
Nov 26 '18 at 8:15





@lowzaii Not really. You'd do best if you add it to your question, as mentioned in comment. If you cannot share actual code you can just include important parts or write simmilar example.

– Michał Powłoka
Nov 26 '18 at 8:15













Okay, I've updated my code. @Michal Powloka

– low zaii
Nov 26 '18 at 8:24





Okay, I've updated my code. @Michal Powloka

– low zaii
Nov 26 '18 at 8:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476189%2finitialize-database-using-kotlin-in-asynctask%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)