android - 从文件选择器中选择文件后返回主活动的活动
问题描述
就我而言,我打开文件选择器并选择一个文本文件以将数据导入数据库。现在我正在测试计数线。我得到了确切的行号,但问题是在选择文件并计算行数后,我的应用程序会自动返回主页。并且警报对话框也被关闭。我不知道发生了什么。这是我当前的代码。
class Admin : AppCompatActivity() {
internal lateinit var lbl: TextView
internal lateinit var db: DataBaseHelper
internal lateinit var btnimport: ImageView
val requestcode=1
internal lateinit var scan: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_admin)
val btn_pw = findViewById<Button>(R.id.btn_pwd)
val btn_location = findViewById<Button>(R.id.btn_location)
val btn_import = findViewById<Button>(R.id.btn_import)
val btn_export = findViewById<Button>(R.id.btn_export)
btn_pw.setOnClickListener{
dialogLogin()
}
btn_location.setOnClickListener {
val intent = Intent(this,Search::class.java)
startActivity(intent)
}
btn_import.setOnClickListener {
importDialog(R.style.DialogSlide,this)
}
}
private fun dialogLogin(){
val builder = AlertDialog.Builder(this)
val inflater = this.layoutInflater
val view = inflater.inflate(R.layout.activity_password,null)
builder.setView(view)
val dialog: AlertDialog = builder.create()
dialog.window?.attributes?.windowAnimations = R.style.DialogSlide
dialog.setMessage("Please Fill The Branch Name")
dialog.show()
val brn_save =view.findViewById<Button>(R.id.btn_save)
val edt_pw = view.findViewById<EditText>(R.id.edt_pw)
brn_save.setOnClickListener{
branch = edt_pw.text.toString()
if(edt_pw.text == null){
Toast.makeText(this,"Please fill the branch",Toast.LENGTH_SHORT).show()
}
else{
password = edt_pw.text.toString()
dialog.dismiss()
setpwd(password.toString())
real_pwd = password.toString()
}
}
}
private fun setpwd( v:String) {
var editor=getSharedPreferences("yo", MODE_PRIVATE).edit()
editor.putString("val", v)
editor.apply()
}
/*import text file 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()
dialog.setCancelable(false)
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."
}
}
}
@SuppressLint("MissingSuperCall")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (data == null)
return
if (requestCode==requestcode) {
val intent=Intent(this, MainActivity::class.java)
startActivity(intent)
val filepath=data.data
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())
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()
}
}
}
}
解决方案
看起来你是故意去那里的
if (requestCode==requestcode) {
val intent=Intent(this, MainActivity::class.java)
startActivity(intent)
...
...
推荐阅读
- android - 在前台服务中使用 LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY 时是否有任何限制?
- java - 从java中的SQL解密数据
- maven - 如何在不依赖项目的情况下解决 maven pom 中工件的路径?
- scala - Why is override sometimes required for an abstract method?
- .htaccess - 如何使用 Htaccess 在 php 中将页面重写为带有 id 的基本 url
- angular - IntersectionObserver 模糊延迟图像加载
- c++ - ESP-IDF 如何检查任务是否已经在运行?
- python - Django:./manage.py [...] 和 python manage.py [...] 有什么区别?
- mysql - 两个不同的列需要具有 SUM 条件的条件
- dagger-hilt - 迁移到 2.31.2-alpha 后 Hilt java.lang.reflect.InvocationTargetException