简体   繁体   中英

Kotlin change property value

I have generic function, which should bind universal object to views. I already handle getting data from objects and get fields name from view. But now I have a problem with assign this data.

This function iterate for every element in layout and get id, then call get on object function (the name of layout element have this same names as in object). In IF statement I want to assign the objectValue (String) to the 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())
            }
        }
    }

I have property mText in bindingAny and then in date but how I can access the mText property in this? 在此处输入图像描述

Next I try to change this data by binding.fields I can recive Field object which contains type but I dont find any opportunity to change text 在此处输入图像描述

Also I try to access to method function setText(charSequence) but I get a cast problem

#EDIT My solution

    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
        }
    }

if someone looking for answer. I found the way how it's possible to handle this problem. I just take binding.root and cast it to ViewGroup,

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

and this way I have access to views, now I have search all TextViews like in binary tree but with n leaf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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