hi I'm getting information from web with jsoup and coroutine and I want to show data in recyclerview All the information is well received but the RecyclerView does not show anything and the view is not updated
fun myCoroutine(): ArrayList<DataModel> {
val listx = arrayListOf<DataModel>()
GlobalScope.launch { // launch new coroutine in background and continue
Log.d("asdasdasd", "start")
var doc: Document = Jsoup.connect("http://5743.zanjan.medu.ir").timeout(0).maxBodySize(0).ignoreHttpErrors(true).sslSocketFactory(setTrustAllCerts()).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")
listx.add(DataModel(cols.get(2).text(),strhref))
Log.d("asdasf",cols.get(2).text())
}
}
}
return listx
}
private fun getData() {
itemsData = ArrayList()
itemsData = myCoroutine()
adapter.notifyDataSetChanged()
adapter = RVAdapter(itemsData)
}
and this is oncreate
var itemsData = ArrayList<DataModel>()
adapter = RVAdapter(itemsData)
val llm = LinearLayoutManager(this)
itemsrv.setHasFixedSize(true)
itemsrv.layoutManager = llm
getData()
itemsrv.adapter = adapter
This code has numerous bugs (getData
, for instance, never sets the adapter onto the RecyclerView
), but the biggest issue is that you're not actually waiting for listx
to be populated - you're returning it immediately before it's populated. You need to either move the population of the adapter to the coroutine and run that part on the UI thread dispatcher, or use a callback, or dispatch it to the UI thread. Launching a coroutine and returning immediately doesn't make the data get populated when something tries to use it.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加