F
F
foonfyrick2020-09-19 08:36:21
Android
foonfyrick, 2020-09-19 08:36:21

I created a class that extends LiveData, why can't I apply ExampleClass.value=edText.text.toString in MainActivity?

class MyLiveData: LiveData<String>() {
   
}

Main
class MainActivity : AppCompatActivity() {
    val myLiveData=MyLiveData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn_Ok.setOnClickListener {
            myLiveData.value = edText.text.toString() //  ошибка
            Log.e("//////////////////","${myLiveData.value}")
        }
    }
}

But if I create my method in MyLiveData class,
fun getValue(s:String){
       value=s
    }

Then myLiveData.getValue(edText.text.toString()) writes the value to the base, why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-09-19
@foonfyrick

Because it is necessary to read errors and the documentation.
setValue is a protected method. It can only be called from subclass methods, not from outside.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question