首页 > 解决方案 > Kotlin ViewModel List of String

问题描述

I am new to Kotlin and ViewModel uses. I am trying to create a simple ViewModel which got a List of String. The idea is to update a list a picture path (String) in this ViewModel.

My issue is that my ViewModel is not updating. It only add 1 entry.

class PicturesViewModel : ViewModel() {

    var pictureListLive: MutableLiveData<MutableList<String>> = MutableLiveData()

    var list = ArrayList<String>()

    fun addPictureToList(picture: String) {
        list.add(picture)
        pictureListLive.value = list
    }
}

In my activity I initialize the viewModel and observe like that :

 private fun configureViewModel(){
        this.picturesViewModel = ViewModelProviders.of(this).get(PicturesViewModel::class.java!!)
        this.picturesViewModel.pictureListLive.observe(this, Observer{
            if (it != null) {
                Log.i("Pictures",it.size.toString())
            }
        })
    }

And I add one String to the ViewModel :

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_CAPTURE) {
            picturesViewModel.addPictureToList(currentPhotoPath)
            val intentPreview = Intent(this,PreviewActivity::class.java)
            intentPreview.putExtra("Picture",currentPhotoPath)
            startActivity(intentPreview)}

        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CHOOSE_PHOTO){
            this.pictureSelectedPath = data?.data.toString()
            Log.i("Photo",pictureSelectedPath)
        }
    }

The ActivityOnResult come from after taking a picture to the camera. I want to update the list of String of the ViewModel to add the picture camera path after taking few pictures.

When I am using it like that today the Size of the ViewModel list remains to 1. I think only the last one is in it.

Thanks for your help.

标签: androidkotlin

解决方案


This is a use case in initializing and using a simple ViewModel

//in your pictures fragment
private lateinit var picturesViewModel: PicturesViewModel

than in your onCreate

picturesViewModel= ViewModelProviders.of(this).get(PicturesViewModel::class.java)

After you have initialized the viewmodel , you may observe data changes that happen there , below the initialisation of course:

picturesViewModel.pictureListLive.observe(this, Observer {
            //list changes are handled here 
        })

If I were to rewrite you ViewModelClass it would be like this :

class PictureViewModel : ViewModel() {

    private var pictureListLive: MutableLiveData<MutableList<String>> = MutableLiveData()


    fun setPictureList(picture: String) {
       val list = ArrayList<String>()
       list.add(picture)
       pictureListLive.value = list 
    }
}

There is no need to return a list because the MutableLiveData work in the publish-subscribe logic , aka the Observable Pattern .

This is similar like EventBus or RxJava operators

Note

I would need more information about that setPictureList method because like that it doesn't have much sense . Or maybe you wanna call it somewhere else or idk .


推荐阅读