处理LiveData和Observable以在函数内部返回所赋予的值

问题描述

我从MVVM架构和Kotlin反应式编程开始。 仅在用户单击par1或par2按钮以将内容返回到LiveData,Coroutines and/or Observable函数的第二位后,如何才能使用kotlin optionSelected()insertionSort()函数中执行返回操作?

class PlaceholderFragment : Fragment()  {
    var par1:Button? = null
    var par2:Button? = null
    var parTextLive: LiveData<String> = MutableLiveData<String>()

    var arr = arrayOf("homework","chores","shopping")
    var i = -1
    var j: Int = 0
    var tmp: String = ""
    var clicked: Boolean = false
    var parText: String = ""
    var len: Int = arr.size

    override fun onCreateView(
            inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_main,container,false)
        par1 = root!!.findViewById(R.id.par1) as Button
        par2 = root!!.findViewById(R.id.par2) as Button

        lifecycleScope.launch {
            insertionSort()
        }
        par1!!.setOnClickListener {
            lifecycleScope.launch {

                //how to get in LiveData the String value?

                //parTextLive = (par1!!.text.toString())
                parText = (par1!!.text.toString())
            }
        }
        par2!!.setOnClickListener {
            lifecycleScope.launch {

                //how to get in LiveData the String value?

                //parTextLive = (par2!!.text.toString())
                parText = (par2!!.text.toString())
            }
        }
        return root
    }

    suspend fun optionSelected(str1: String,str2: String): String {
        return withContext(Dispatchers.Main) {
            println("opções setadas: $str1 or $str2")
            par1!!.text = str1
            par2!!.text = str2

            // I setted the buttons pair1 and pair2 with texts of str1 and str2. When the user click in one of them,I want to return the text of clicked button 

            delay(5000) //substitute the delay by the user click event

            return@withContext str1  //returning the variable parTextLive with the refreshed value
        }
    }

    suspend fun insertionSort(): Array<String> {
        return withContext(Dispatchers.Default) {
            while(len-- != 0) {
                tmp = arr[++i];
                j = i
                while (j-- != 0 && (optionSelected(arr[j],tmp) == arr[j])) {
                    arr[j + 1] = arr[j];
                }
                arr[j + 1] = tmp
            }
            return@withContext arr.apply { reverse() }
        }
    }
}

解决方法

我使用parTextLive.value为MutableLiveData赋值 为了等待用户单击接收LiveData值,我使用了parTextLive.asFlow().first(),它一直等到parTextLive收到一些值

class PlaceholderFragment : Fragment()  {
    var par1:Button? = null
    var par2:Button? = null
    var parTextLive: LiveData<String> = MutableLiveData<String>()

    var arr = arrayOf("homework","chores","shopping")

    override fun onCreateView(
            inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_main,container,false)
        par1 = root!!.findViewById(R.id.par1) as Button
        par2 = root!!.findViewById(R.id.par2) as Button

        lifecycleScope.launch {
            insertionSort(arr)
        }
        par1!!.setOnClickListener {
            lifecycleScope.launch {    
                parTextLive.value = (par1!!.text.toString())
            }
        }
        par2!!.setOnClickListener {
            lifecycleScope.launch {    
                parTextLive.value = (par2!!.text.toString())
            }
        }
        return root
    }

    suspend fun optionSelected(str1: String,str2: String): String {
        return withContext(Dispatchers.Main) {
            println("opções setadas: $str1 or $str2")
            par1!!.text = str1
            par2!!.text = str2

            val parText = (parTextLive.asFlow().first())
            parTextLive = MutableLiveData<String>()
            return@withContext parText
        }
    }

    suspend fun insertionSort(): Array<String> {
        return withContext(Dispatchers.Default) {
            while(len-- != 0) {
                tmp = arr[++i];
                j = i
                while (j-- != 0 && (optionSelected(arr[j],tmp) == arr[j])) {
                    arr[j + 1] = arr[j];
                }
                arr[j + 1] = tmp
            }
            return@withContext arr.apply { reverse() }
        }
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...