Created
December 15, 2017 22:11
-
-
Save GabriellCosta/0db5ab6b284c7f013c104f932fe41e01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.gabriel.mediatorlivedata | |
import android.arch.lifecycle.LiveData | |
import android.arch.lifecycle.MediatorLiveData | |
import android.arch.lifecycle.MutableLiveData | |
import android.os.Handler | |
import android.util.Log | |
import java.util.* | |
class ApiClass { | |
val list = listOf("Gabriel", "Miguel") | |
fun getApi() : LiveData<List<String>>{ | |
val mutableLiveData = MutableLiveData<List<String>>() | |
mutableLiveData.value = Collections.emptyList() | |
Handler().postDelayed({ mutableLiveData.value = list }, 3000) | |
return mutableLiveData | |
} | |
} | |
class Caller { | |
fun caller() : MediatorLiveData<List<String>>{ | |
val apiClass = ApiClass() | |
val mediatorLiveData = MediatorLiveData<List<String>>() | |
mediatorLiveData.addSource(apiClass.getApi(), { Log.i("myTag", "Works") }) | |
return mediatorLiveData | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.gabriel.mediatorlivedata | |
import android.arch.lifecycle.Observer | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val caller = Caller().caller() | |
caller.observe(this, Observer { Log.i("Activity", "zica") }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment