Recyclerview not updating in coroutine

user11453013

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
Ryan M

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Updating RecyclerView On Firebase Database onDataChange

分類Dev

Updating an ImageView in a RecyclerView on item click

分類Dev

Android recyclerview not updating when removing an item

分類Dev

Tornado coroutine

分類Dev

Coroutine is a class in its nature?

分類Dev

How to properly exec() a coroutine

分類Dev

PlayMode Tests with Coroutine SetUp

分類Dev

try/except/finally in Coroutine

分類Dev

Lua segfault on coroutine resume

分類Dev

How to rate limit a coroutine and re-call the coroutine after the limit?

分類Dev

Coroutine vs Fiber difference clarification

分類Dev

Can't stop a Coroutine in Unity

分類Dev

Set a class variable in IEnumerator coroutine

分類Dev

RecyclerViewのRecyclerView:ItemDecoration

分類Dev

UITableView not updating

分類Dev

@ asyncio.coroutineとasync def

分類Dev

How to call Kotlin coroutine in composable function callbacks?

分類Dev

How do I kill a task / coroutine in Julia?

分類Dev

Why do I need to await for a coroutine?

分類Dev

Kotlin alternative to Python's coroutine yield and send

分類Dev

how to add a coroutine to running event loop?

分類Dev

boost :: coroutine2 vs CoroutineTS

分類Dev

Lua C API - attaching data to a coroutine

分類Dev

Kotlin coroutine list returning null value

分類Dev

Coroutine testing fails with "This job has not completed yet"

分類Dev

Returning from inner nested coroutine by label Kotlin

分類Dev

Android: Coroutine doesn't always work on call

分類Dev

How would you make a monad instance of this coroutine?

分類Dev

why the coroutine exception handler double the original exception?