Android架构组件:ViewModel在配置更改时返回null

时间:2018-02-05 01:22:48

标签: android kotlin android-architecture-components

我正在使用pixabay api在kotlin开发一个图库应用程序,实现Android架构组件并进行改造以与服务器进行交互。 该应用程序工作正常,但当我更改配置时,视图模型返回null!有什么问题?

interface PhotoApi {
    @GET(context.getString(R.string.api_key))
    fun getPhotos(): Call<PhotoList>
}

改造设置

object PhotoRetriever {
    val BASE_URL = "https://pixabay.com/api/"
    val service: PhotoApi

    init {
        val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build()
        service = retrofit.create(PhotoApi::class.java)
    }

}
class MainActivity : AppCompatActivity() {
    //var photoList: List<Photo>? = null
    lateinit var viewModel: MainActivityViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        viewModel = ViewModelProviders.of(this@MainActivity).get(MainActivityViewModel::class.java)
        viewModel.getList().observe(this, object :Observer<List<Photo>> {
            override fun onChanged(t: List<Photo>?) {
                    textview.setText(t?.get(1)?.webFormatUrl)
            }
        })

视图模型类

class MainActivityViewModel : ViewModel() {

    private val repository: Repository
    lateinit var photoList: LiveData<List<Photo>>
    val itemsListObservable: MediatorLiveData<List<Photo>>


    init {
        repository = Repository()
        itemsListObservable = MediatorLiveData()
    }

    fun getList(): LiveData<List<Photo>> {

       photoList = repository.getNetworkData()
        Log.i("RECEIVED","size is ${photoList.value?.size} from viewModel ")
        return photoList
    }
}

存储库类

class Repository {
    val photoApi: PhotoApi
    var photoList: List<Photo>?
    val retlist = MutableLiveData<List<Photo>>()

    init {
        photoApi = PhotoRetriever.service
        photoList = ArrayList<Photo>()
    }

    fun getItemsListFromWeb() {
        photoApi.getPhotos().enqueue(object : Callback<PhotoList> {
            override fun onFailure(call: Call<PhotoList>?, t: Throwable?) {
                // Log.i("RECEIVED","${t?.message} from repository ")
                // retlist.value = null
            }

            override fun onResponse(call: Call<PhotoList>?, response: Response<PhotoList>?) {
                if (response!!.isSuccessful) {
                    retlist.value = response.body()?.hits
                } else {
                    retlist.value = null
                }
                Log.i("RECEIVED", "size is ${retlist.value?.size} from repository ")
            }

        })

    }

    fun getNetworkData(): LiveData<List<Photo>> {
        getItemsListFromWeb()
        return retlist
    }

}

1 个答案:

答案 0 :(得分:0)

我已经解决了问题,问题在于改进onResponse回调方法 我删除了else块,一切正常

之前

 override fun onResponse(call: Call<PhotoList>?, response: Response<PhotoList>?) {
                if (response!!.isSuccessful) {
                    retlist.value = response.body()?.hits
                } else {
                    retlist.value = null
                }
                Log.i("RECEIVED", "size is ${retlist.value?.size} from repository ")
            }

<强>后

 override fun onResponse(call: Call<PhotoList>?, response: Response<PhotoList>?) {
                    if (response!!.isSuccessful) {
                        retlist.value = response.body()?.hits
                    } 
                    Log.i("RECEIVED", "size is ${retlist.value?.size} from repository ")
                }