cat_dog_orange 2023-05-17 10:26 采纳率: 36.4%
浏览 15
已结题

在AS中使用mysql5.1.49驱动连接阿里云服务器上mysql5.7.4的数据库,可以查询到数据库中的数据,但是无法显示在界面上

在AS中使用mysql5.1.49驱动连接阿里云服务器上mysql5.7.4的数据库,可以查询到数据库中的数据,但是无法显示在界面上,求大家指导:
查询数据相关代码:

private fun initInfo() {
        Thread {
            val conn: Connection = JDBCUtil.connection
            val sql = "select * from PlantInfo"
            val stmt = conn.createStatement()
            val rs : ResultSet = stmt.executeQuery(sql)
            while (rs.next()) {
                val pic = rs.getString("plantPicture")
                val name = rs.getString("plantName")
                val person = rs.getString("takePhotoPerson")
                val time = rs.getString("takePhotoDate") + " " + rs.getString("takePhotoTime")
                Log.d("InfoSheet查询数据库结果", "plantInfo name is $name")
                val infoStore = InfoQuery(pic, name, person, time)
                infoQueryList.add(infoStore)
            }
            try {
                rs.close()
            } catch (e: SQLException) {
                e.printStackTrace()
            }
            try {
                stmt.close()
            } catch (e: SQLException) {
                e.printStackTrace()
            }
            try {
                conn.close()
            } catch (e: SQLException) {
                e.printStackTrace()
            }
        }.start()
    }

将数据显示到界面上的相关代码:

class InfoQueryAdapter(private val context: Context, private val infoQueryList: List<InfoQuery>): RecyclerView.Adapter<InfoQueryAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val infoPic: ImageView = view.findViewById(R.id.infoPic)
        val infoName: TextView = view.findViewById(R.id.infoName)
        val infoPerson: TextView = view.findViewById(R.id.infoPerson)
        val infoTime: TextView = view.findViewById(R.id.infoTime)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.list_item_layout,
            parent,
            false
        )
        val viewHolder = ViewHolder(view)
        //RecyclerView点击事件
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val infoQuery = infoQueryList[position]
//            Toast.makeText(parent.context, "即将查看该条目详细信息", Toast.LENGTH_SHORT).show()
            val intent = Intent(parent.context, SpecificInfo::class.java)
            intent.putExtra("plantName", infoQuery.nameInformation) // 将从数据库中取出的plantName信息传递给将要跳转的intent
            intent.putExtra("plantTime", infoQuery.timeInformation)
            parent.context.startActivity(intent)
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val infoQuery = infoQueryList[position]
        Glide.with(context).load(infoQuery.image).into(holder.infoPic)
        holder.infoName.text = String.format(context.getString(R.string.plantNameInfo), infoQuery.nameInformation)
        holder.infoPerson.text = String.format(context.getString(R.string.remarkPerson), infoQuery.personInformation)
        holder.infoTime.text = String.format(context.getString(R.string.remarkDate), infoQuery.timeInformation)
    }
    override fun getItemCount() = infoQueryList.size
}

可以查询出数据,但是无法显示在界面中:

D/InfoSheet查询数据库结果: plantInfo name is 牡丹
    plantInfo name is 欧洲雪球
D/InfoSheet查询数据库结果: plantInfo name is 樱花
    plantInfo name is 111
    plantInfo name is 111

img

  • 写回答

2条回答 默认 最新

  • AllenGd 大数据领域优质创作者 2023-05-17 10:44
    关注

    引入ChatGPT部分内容参考:
    可能是因为在查询数据后,没有在主线程中更新UI导致无法显示在界面上。可以尝试在查询数据后,使用runOnUiThread方法更新UI,或者使用Handler机制更新UI。
    修改initInfo()方法如下:

    private fun initInfo() { 
    Thread { 
    val conn: Connection = JDBCUtil.connection 
    val sql = "select * from PlantInfo" 
    val stmt = conn.createStatement() 
    val rs : ResultSet = stmt.executeQuery(sql) 
    while (rs.next()) { 
    val pic = rs.getString("plantPicture") 
    val name = rs.getString("plantName") 
    val person = rs.getString("takePhotoPerson") 
    val time = rs.getString("takePhotoDate") + " " + rs.getString("takePhotoTime") 
    Log.d("InfoSheet查询数据库结果", "plantInfo name is $name") 
    val infoStore = InfoQuery(pic, name, person, time) 
    infoQueryList.add(infoStore) 
    }
     try { 
    rs.close() 
    } catch (e: SQLException) { 
    e.printStackTrace() 
    } 
    try { stmt.close() 
    } catch (e: SQLException) {
     e.printStackTrace() }
     try { conn.close()
     } catch (e: SQLException) {
     e.printStackTrace() } // 在主线程中更新UI 
    runOnUiThread { infoQueryAdapter.notifyDataSetChanged() } }.start() }
    

    在查询数据后,使用runOnUiThread方法更新UI,调用infoQueryAdapter.notifyDataSetChanged()方法通知RecyclerView更新数据。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月18日
  • 创建了问题 5月17日

悬赏问题

  • ¥15 在国外文献网站里点击view pdf 加载异常缓慢甚至加载不出来。
  • ¥50 python批量提取发票的信息
  • ¥15 mysql安装,初始化数据库失败
  • ¥15 虚幻五引擎内容如何上传至网盘?
  • ¥15 使用mmpose库时出现了问题
  • ¥15 IRI2016模型matlab运行报错
  • ¥50 bat怎么设置电脑后台自动点击网页指定词运行脚本,输入指定网页链接,指定点击词,指定间隔时间,指定网页出现的词,指定网页出现词出现后后点击锁定,放在后台运行不影响前台鼠标工作
  • ¥20 20CrMnMo的高温变形抗力
  • ¥15 RTX3.6 5565驱动中断报错
  • ¥50 带防重放token(Antireplay-Token)的网站怎么用Python发送请求