如何在将数据导入数据库时使用进度条?
How to work with progress bar while importing data to database?
就我而言,我需要将大量数据从文本文件导入数据库。在导入数据时,我想显示一些进度条以便让用户知道。我想知道如何在我的案例中实现进度条。现在我可以导入数据并获取总行数。这是当前代码
private fun importDialog(type: Int,context: Context) {
val builder=AlertDialog.Builder(this)
val inflater=this.layoutInflater
val view=inflater.inflate(R.layout.import__dialog, null)
builder.setView(view)
val dialog: AlertDialog=builder.create()
dialog.window?.attributes?.windowAnimations=type
dialog.setMessage(context.getString(R.string.open_master))
lbl=EditText(this)
lbl=view.findViewById(R.id.edit_master)
lbl.text=noti.toString()
dialog.show()
db=DataBaseHelper(this)
btnimport=view.findViewById(R.id.img_import)
btnimport.setOnClickListener {
val fileintent=Intent(Intent.ACTION_GET_CONTENT)
fileintent.type="txt/csv"
try {
startActivityForResult(fileintent, requestcode)
dialog.show()
} catch (e: ActivityNotFoundException) {
lbl.text="No activity can handle picking a file. Showing alternatives."
}
}
scan=view.findViewById<Button>(R.id.btn_scan)
scan.setOnClickListener {
import(this)
}
}
@SuppressLint("MissingSuperCall")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (data == null)
return
if (requestCode==requestcode) {
val filepath=data.data
file = filepath.toString()
val cursor=contentResolver.openInputStream(android.net.Uri.parse(filepath.toString()))
lbl.text=filepath.toString()
master_path=filepath.toString()
noti=cursor.toString()
val db=this.openOrCreateDatabase("database.db", Context.MODE_PRIVATE, null)
val tableName="Master"
db.execSQL("delete from $tableName")
val text = StringBuilder()
try {
println("gg")
if (resultCode == Activity.RESULT_OK) {
try {
val file=InputStreamReader(cursor)
var lineCount = 0
val buffer=BufferedReader(file)
buffer.readLine()
val contentValues=ContentValues()
db.beginTransaction()
while(true) {
val line=buffer.readLine()
if (line == null) break
lineCount++
}
println(lineCount.toString())
line = lineCount.toString().toInt()
db.setTransactionSuccessful()
db.endTransaction()
} catch (e: IOException) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(e.message.toString() + "first")
d.show()
}
} else {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle("Only CSV files allowed")
d.show()
}
} catch (ex: Exception) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(ex.message.toString() + "second")
d.show()
}
}
}
fun import(context: Context) {
val filepath=file
val cursor=contentResolver.openInputStream(android.net.Uri.parse(filepath.toString()))
lbl.text=filepath.toString()
master_path=filepath.toString()
noti=cursor.toString()
val db=this.openOrCreateDatabase("database.db", Context.MODE_PRIVATE, null)
val tableName="Master"
db.execSQL("delete from $tableName")
val text = StringBuilder()
try {
println("gg")
try {
val file=InputStreamReader(cursor)
var lineCount = 0
val buffer=BufferedReader(file)
buffer.readLine()
val contentValues=ContentValues()
db.beginTransaction()
while(true) {
lineCount++
in_line = lineCount
val line=buffer.readLine()
if (line == null) break
val article_array = ArrayList<String>()
var desc_array = ArrayList<String>()
var price_array = ArrayList<String>()
var isbn_array = ArrayList<String>()
var total_array = ArrayList<String>()
for(i in 0..12){
val article = line[i].toString().replace(" ","")
article_array.add(article)
}
for(i in 13..62){
val desc = line[i].toString()
desc_array.add(desc)
}
for(i in 63..70){
val price = line[i].toString().replace(" ","")
price_array.add(price)
}
for(i in 71..line.length-1){
val isbn = line[i].toString().replace(" ","")
isbn_array.add(isbn)
}
val article_list = article_array.toString().replace(", ","").replace("[","").replace("]","")
val desc_list = desc_array.toString().replace(", ","").replace("[","").replace("]","")
val price_list = price_array.toString().replace(", ","").replace("[","").replace("]","")
val isbn_list:String = isbn_array.toString().replace(", ","").replace("[","").replace("]","")
total_array.add(article_list)
total_array.add(desc_list)
total_array.add(price_list)
total_array.add(isbn_list)
val article=total_array[0].toString()
val desc=total_array[1].toString()
val price=total_array[2].toString()
val isbn=total_array[3].toString()
contentValues.put("article", article)
contentValues.put("description", desc)
contentValues.put("price", price)
contentValues.put("isbn", isbn)
db.insert(tableName, null, contentValues)
}
db.setTransactionSuccessful()
db.endTransaction()
} catch (e: IOException) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(e.message.toString() + "first")
d.show()
}
} catch (ex: Exception) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(ex.message.toString() + "second")
d.show()
}
}
请尝试
private ProgressBar progressBar;
private fun importDialog(type: Int,context: Context) {
//
..
//
lbl=view.findViewById(R.id.edit_master)
progressBar = view.findViewById(R.id.progress_bar)
btnimport.setOnClickListener {
progressBar.setVisibility(View.VISIBLE);
}
}
@SuppressLint("MissingSuperCall")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
progressBar.setVisibility(View.GONE);
}
你只需hide/show进度条。
就我而言,我需要将大量数据从文本文件导入数据库。在导入数据时,我想显示一些进度条以便让用户知道。我想知道如何在我的案例中实现进度条。现在我可以导入数据并获取总行数。这是当前代码
private fun importDialog(type: Int,context: Context) {
val builder=AlertDialog.Builder(this)
val inflater=this.layoutInflater
val view=inflater.inflate(R.layout.import__dialog, null)
builder.setView(view)
val dialog: AlertDialog=builder.create()
dialog.window?.attributes?.windowAnimations=type
dialog.setMessage(context.getString(R.string.open_master))
lbl=EditText(this)
lbl=view.findViewById(R.id.edit_master)
lbl.text=noti.toString()
dialog.show()
db=DataBaseHelper(this)
btnimport=view.findViewById(R.id.img_import)
btnimport.setOnClickListener {
val fileintent=Intent(Intent.ACTION_GET_CONTENT)
fileintent.type="txt/csv"
try {
startActivityForResult(fileintent, requestcode)
dialog.show()
} catch (e: ActivityNotFoundException) {
lbl.text="No activity can handle picking a file. Showing alternatives."
}
}
scan=view.findViewById<Button>(R.id.btn_scan)
scan.setOnClickListener {
import(this)
}
}
@SuppressLint("MissingSuperCall")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (data == null)
return
if (requestCode==requestcode) {
val filepath=data.data
file = filepath.toString()
val cursor=contentResolver.openInputStream(android.net.Uri.parse(filepath.toString()))
lbl.text=filepath.toString()
master_path=filepath.toString()
noti=cursor.toString()
val db=this.openOrCreateDatabase("database.db", Context.MODE_PRIVATE, null)
val tableName="Master"
db.execSQL("delete from $tableName")
val text = StringBuilder()
try {
println("gg")
if (resultCode == Activity.RESULT_OK) {
try {
val file=InputStreamReader(cursor)
var lineCount = 0
val buffer=BufferedReader(file)
buffer.readLine()
val contentValues=ContentValues()
db.beginTransaction()
while(true) {
val line=buffer.readLine()
if (line == null) break
lineCount++
}
println(lineCount.toString())
line = lineCount.toString().toInt()
db.setTransactionSuccessful()
db.endTransaction()
} catch (e: IOException) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(e.message.toString() + "first")
d.show()
}
} else {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle("Only CSV files allowed")
d.show()
}
} catch (ex: Exception) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(ex.message.toString() + "second")
d.show()
}
}
}
fun import(context: Context) {
val filepath=file
val cursor=contentResolver.openInputStream(android.net.Uri.parse(filepath.toString()))
lbl.text=filepath.toString()
master_path=filepath.toString()
noti=cursor.toString()
val db=this.openOrCreateDatabase("database.db", Context.MODE_PRIVATE, null)
val tableName="Master"
db.execSQL("delete from $tableName")
val text = StringBuilder()
try {
println("gg")
try {
val file=InputStreamReader(cursor)
var lineCount = 0
val buffer=BufferedReader(file)
buffer.readLine()
val contentValues=ContentValues()
db.beginTransaction()
while(true) {
lineCount++
in_line = lineCount
val line=buffer.readLine()
if (line == null) break
val article_array = ArrayList<String>()
var desc_array = ArrayList<String>()
var price_array = ArrayList<String>()
var isbn_array = ArrayList<String>()
var total_array = ArrayList<String>()
for(i in 0..12){
val article = line[i].toString().replace(" ","")
article_array.add(article)
}
for(i in 13..62){
val desc = line[i].toString()
desc_array.add(desc)
}
for(i in 63..70){
val price = line[i].toString().replace(" ","")
price_array.add(price)
}
for(i in 71..line.length-1){
val isbn = line[i].toString().replace(" ","")
isbn_array.add(isbn)
}
val article_list = article_array.toString().replace(", ","").replace("[","").replace("]","")
val desc_list = desc_array.toString().replace(", ","").replace("[","").replace("]","")
val price_list = price_array.toString().replace(", ","").replace("[","").replace("]","")
val isbn_list:String = isbn_array.toString().replace(", ","").replace("[","").replace("]","")
total_array.add(article_list)
total_array.add(desc_list)
total_array.add(price_list)
total_array.add(isbn_list)
val article=total_array[0].toString()
val desc=total_array[1].toString()
val price=total_array[2].toString()
val isbn=total_array[3].toString()
contentValues.put("article", article)
contentValues.put("description", desc)
contentValues.put("price", price)
contentValues.put("isbn", isbn)
db.insert(tableName, null, contentValues)
}
db.setTransactionSuccessful()
db.endTransaction()
} catch (e: IOException) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(e.message.toString() + "first")
d.show()
}
} catch (ex: Exception) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(ex.message.toString() + "second")
d.show()
}
}
请尝试
private ProgressBar progressBar;
private fun importDialog(type: Int,context: Context) {
//
..
//
lbl=view.findViewById(R.id.edit_master)
progressBar = view.findViewById(R.id.progress_bar)
btnimport.setOnClickListener {
progressBar.setVisibility(View.VISIBLE);
}
}
@SuppressLint("MissingSuperCall")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
progressBar.setVisibility(View.GONE);
}
你只需hide/show进度条。