LiveData not Observing after first Call

后端 未结 1 957
花落未央
花落未央 2021-01-27 11:49

I implemented LiveData & ViewModel to mimic AsyncTaskLoader.

I load file names from the camera directory in DCIM, and then i attach a fileObserver to Observe when a

1条回答
  •  温柔的废话
    2021-01-27 12:45

    I have here an example @Micklo_Nerd but it does not use your problem of getting the files deleted but it gives you the idea for what you need to do. In my example the user insert a name and after clicking a button, the list is changed.

    activity_main.xml

    
    
    
    

    MyRepository (In your example is the MyLiveData)

    In here you must do the work of getting the filenames in the folder, and put the in the MutableLiveData.

    class MyRepository {
    
        fun loadFileNames(liveData : MutableLiveData>, filename: String){
    
           var fileList : MutableList? = liveData.value
    
           if(test == null)
               fileList = MutableList(1){ filename }
           else
              fileList.add(filename)
    
           liveData.value = fileList
         }
    
    }
    

    MyViewModel

    In here, I have two methods: one to update the list as I click the button and another to get the list of file names. You should probably only need the one that gets the list

    class MyViewModel : ViewModel() {
        val repo: MyRepository
        var mutableLiveData : MutableLiveData>
    
    
        init {
           repo = MyRepository()
           mutableLiveData = MutableLiveData()
        }
    
        fun changeList(filename: String){
    
           repo.loadFileNames(mutableLiveData, filename)
        }
    
        fun getFileList() : MutableLiveData>{
    
          return mutableLiveData
        }
    }
    

    MainActivity

    In here, you see I am observing the method that returns the list of filenames, which is what you need to do, because that is what is going to change.

    class MainActivity : AppCompatActivity(), View.OnClickListener {
    
       private val TAG = "MyFragment"
    
       private lateinit var viewModel: MyViewModel
    
       override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
    
          viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
    
          viewModel.getFileList().observe(this, Observer> { fileNames ->
             Log.d(TAG, "New Live Data Dispatch")
    
             textView.text = ""
    
             for ((index, name) in fileNames!!.withIndex()) {
                textView.append("the element at $index is $name\n")
             }
          })
    
          buttonAdd.setOnClickListener(this)
       }
    
       override fun onClick(v: View?) {
          when(v!!.id){
            R.id.buttonAdd -> {
    
                viewModel.changeList(filename.text.toString())
                filename.text.clear()
            }
          }
       }
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题