在片段和协程中使用 Recyclerview 不起作用
using Recyclerview in fragment and coroutines not work
你好我在片段和协程中使用了 recyclerview,但是 recyclerview 没有更新并且是空的
这是我从网络获取数据的代码,我在 OnCreateView 中编写了它
launch {
val operation = async(Dispatchers.IO) {
Log.d("asdasdasd", "start")
var doc: Document = Jsoup.connect("http://5743.zanjan.medu.ir").timeout(0).maxBodySize(0).ignoreHttpErrors(true).get()
val table: Elements = doc.select("table[class=\"table table-striped table-hover\"]")
for (myTable in table) {
val rows: Elements = myTable.select("tr")
for (i in 1 until rows.size) {
val row: Element = rows.get(i)
val cols: Elements = row.select("td")
val href: Elements = row.select("a")
val strhref: String = href.attr("href")
itemsData.add(CircularModel(cols.get(2).text(),strhref,"2019",""))
}
}
}
operation.await() // wait for result of I/O operation without blocking the main thread
// update views
activity?.runOnUiThread{
adapter = CircularAdapter(itemsData, this@CircularFragment)
adapter.notifyDataSetChanged()
rc.adapter = adapter
}
}
我检查了一下,itemsData 不为空,但我不知道为什么 recyclerview 没有更新
将适配器设置为 Recycler 后尝试调用 adapter.notifyDataSetChanged()
尝试使用 withContext(Dispatchers.Main)
而不是 activity?.runOnUiThread{
语法从协程访问主线程
你好我在片段和协程中使用了 recyclerview,但是 recyclerview 没有更新并且是空的
这是我从网络获取数据的代码,我在 OnCreateView 中编写了它
launch {
val operation = async(Dispatchers.IO) {
Log.d("asdasdasd", "start")
var doc: Document = Jsoup.connect("http://5743.zanjan.medu.ir").timeout(0).maxBodySize(0).ignoreHttpErrors(true).get()
val table: Elements = doc.select("table[class=\"table table-striped table-hover\"]")
for (myTable in table) {
val rows: Elements = myTable.select("tr")
for (i in 1 until rows.size) {
val row: Element = rows.get(i)
val cols: Elements = row.select("td")
val href: Elements = row.select("a")
val strhref: String = href.attr("href")
itemsData.add(CircularModel(cols.get(2).text(),strhref,"2019",""))
}
}
}
operation.await() // wait for result of I/O operation without blocking the main thread
// update views
activity?.runOnUiThread{
adapter = CircularAdapter(itemsData, this@CircularFragment)
adapter.notifyDataSetChanged()
rc.adapter = adapter
}
}
我检查了一下,itemsData 不为空,但我不知道为什么 recyclerview 没有更新
将适配器设置为 Recycler 后尝试调用 adapter.notifyDataSetChanged()
尝试使用 withContext(Dispatchers.Main)
而不是 activity?.runOnUiThread{
语法从协程访问主线程