简体   繁体   English

如何从Thread返回值?

[英]How to return value from Thread?

I want to pass arrayList to adapter constructor. 我想将arrayList传递给适配器构造函数。 Arraylist is returned by query. Arraylist由查询返回。 Query must be in Thread. 查询必须在Thread中。 How can i do that? 我怎样才能做到这一点?

CostDao.kt CostDao.kt

@Dao
interface CostDAO {

    @Query("select * from Cost")
    fun getAll() : List<Cost>

MainActivity.kt MainActivity.kt



class ComparatorActivity : AppCompatActivity(), OpenDatabase.OpenDatabaseListener {
    private var db: AppDatabase? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        val open = OpenDatabase(this)
        open.setOpenDatabaseListener(this)
        open.load()
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        Threads()


//i need to pass the arraylist
        val adapter = MyAdapter(this, )

        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        recyclerView.adapter = adapter

}
    fun Threads() {
        Thread {
            val List = db!!.costDAO().getAll()
        }.start()
}


How can i return List from Thread? 如何从线程返回List? and then pass it to adapter constructor. 然后将其传递给适配器构造函数。

you can pass an empty list to adapter constructor and when the data retrieved from the database call adapter.notifydatasetchanged(), but the list must be the same object. 您可以将空列表传递给适配器构造函数,并且当从数据库中检索的数据调用adapter.notifydatasetchanged()时,但列表必须是同一个对象。

class ComparatorActivity : AppCompatActivity(), OpenDatabase.OpenDatabaseListener {
    private var db: AppDatabase? = null
    var list = listOf<Cost>()
    private lateinit var adapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        val open = OpenDatabase(this)
        open.setOpenDatabaseListener(this)
        open.load()
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)

        adapter = MyAdapter(this,list)
        Threads()

        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        recyclerView.adapter = adapter
    }
    fun Threads() {
        Thread {
            list = db!!.costDAO().getAll()
            adapter.notifyDataSetChanged()
        }.start()
}

Create a new instance of the Adapter when data is ready and set it to your recyclerView : 在数据准备就绪时创建Adapter的新实例并将其设置为recyclerView

Thread {
    val itemsList...// from db
    runOnUiThread {
        recyclerView.adapter = MyAdapter(this, itemsList)
    }
}.start()

you could alternatively create a setItems method (in your adapter ) and assign it that way: 您也可以创建一个setItems方法(在您的adapter )并以这种方式分配它:

class MyAdapter... {

    private val items = mutableListOf<Cost>()

    fun setItems(items: List<Cost>) {
        this.items.clear()
        this.items.addAll(items)
        notifyDataSetChanged()
    }
}

and

Thread {
    val itemsList...// from db
    runOnUiThread {
        (recyclerView.adapter as MyAdapter).setItems(itemsList)
    }
}.start()

I also suggest using AsyncTask instead of a regular Thread 我还建议使用AsyncTask而不是常规Thread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM