在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

引入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更新数据。