微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决处理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() }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。