簡體   English   中英

Kotlin 更改屬性值

[英]Kotlin change property value

我有通用 function,它應該將通用 object 綁定到視圖。 我已經處理了從對象獲取數據並從視圖中獲取字段名稱。 但是現在我在分配這些數據時遇到了問題。

這個 function 迭代布局中的每個元素並獲取 id,然后在 object function 上調用 get (布局元素的名稱與對象中的名稱相同)。 在 IF 語句中,我想將objectValue (String) 分配給 TextView

class UniversalViewHolder<T : Any>(val bindingAny: ViewDataBinding) : RecyclerView.ViewHolder(bindingAny.root){
    fun bind(elemAny: Any){
        var binding = bindingAny.javaClass
        binding.fields.forEach {
            try {
                val objectValue = readInstanceProperty<String>(elemAny,it.name)
                Log.d(TAG,it.name+" "+objectValue)
                if (it.type == TextView::class.java){
                    //here bind
                }
            }catch (ex: NoSuchElementException){
                Log.e(TAG, "NO ELEMENT AT THIS FIELD "+ it.name)
            }catch (ex:Exception){
                Log.e(TAG,"CRASH AT THIS ELEM: " + it.name+"\n"+ex.stackTraceToString())
            }
        }
    }

我在 bindingAny 中有屬性 mText,然后在日期中,但是我如何才能訪問其中的 mText 屬性? 在此處輸入圖像描述

接下來我嘗試通過binding.fields更改此數據我可以接收包含類型但我沒有找到任何更改文本的機會的字段 object 在此處輸入圖像描述

我也嘗試訪問方法 function setText(charSequence) 但我遇到了演員表問題

#EDIT 我的解決方案

    fun bind(elemAny: Any,context: Context){
        val binding = bindingAny.javaClass

        binding.fields.forEach { field ->
            try {
                val objectValue = readInstanceProperty<String>(elemAny,field.name)
                Log.d(TAG,field.name+" "+objectValue)
                if (field.type == TextView::class.java){

                    arrayTextView.forEach {
                        if(context.resources.getResourceName(it.id).contains(field.name)){
                            it.text = objectValue
                        }
                    }
                }
            }catch (ex: NoSuchElementException){
                Log.e(TAG, "NO ELEMENT AT THIS FIELD "+ field.name)
            }catch (ex:Exception){
                Log.e(TAG,"CRASH AT THIS ELEM: " + field.name+"\n"+ex.stackTraceToString())
            }
        }
    }
    private var arrayTextView : ArrayList<TextView> = ArrayList()
    private fun searchLeaf(root: ViewGroup,context: Context){
        while(true){
            root.forEach {
                if(it::class == MaterialTextView::class || it::class == TextView::class){
                    Log.d(TAG,context.resources.getResourceName(it.id))
                    arrayTextView.add(it as TextView)
//                }else if ((it as ViewGroup).size == 0) {
                }
                else if((it as ViewGroup).size != 0){
                    try{
                        searchLeaf(it as ViewGroup,context)
                    }catch (ex:Exception){ }
                }
            }
        break
        }
    }

如果有人在尋找答案。 我找到了如何處理這個問題的方法。 我只是將 binding.root 轉換為 ViewGroup,

val x = (bindingAny.root as ViewGroup).children.first() as ViewGroup)

這樣我就可以訪問視圖了,現在我可以像在二叉樹中一樣搜索所有 TextViews 但帶有 n 葉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM