Skip to content Skip to sidebar Skip to footer

Livedata Not Observing After First Call

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 File (pict

Solution 1:

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

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:text="Add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/buttonAdd"app:layout_constraintStart_toStartOf="@+id/filename"app:layout_constraintEnd_toEndOf="@+id/filename"android:layout_marginTop="24dp"app:layout_constraintTop_toBottomOf="@+id/filename"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPersonName"android:ems="10"android:id="@+id/filename"android:layout_marginStart="8dp"app:layout_constraintStart_toStartOf="parent"android:layout_marginEnd="8dp"app:layout_constraintEnd_toEndOf="parent"android:layout_marginTop="32dp"app:layout_constraintTop_toTopOf="parent"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/textView"app:layout_constraintStart_toStartOf="parent"android:layout_marginStart="8dp"app:layout_constraintEnd_toEndOf="parent"android:layout_marginEnd="8dp"android:layout_marginTop="16dp"app:layout_constraintTop_toBottomOf="@+id/buttonAdd"/></android.support.constraint.ConstraintLayout>

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.

classMyRepository{

    funloadFileNames(liveData : MutableLiveData<MutableList<String>>, filename: String){

       var fileList : MutableList<String>? = 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

classMyViewModel : ViewModel() {
    val repo: MyRepository
    var mutableLiveData : MutableLiveData<MutableList<String>>


    init {
       repo = MyRepository()
       mutableLiveData = MutableLiveData()
    }

    funchangeList(filename: String){

       repo.loadFileNames(mutableLiveData, filename)
    }

    fungetFileList() : MutableLiveData<MutableList<String>>{

      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.

classMainActivity : AppCompatActivity(), View.OnClickListener {

   privateval TAG = "MyFragment"privatelateinitvar viewModel: MyViewModel

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

      viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)

      viewModel.getFileList().observe(this, Observer<MutableList<String>> { 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)
   }

   overridefunonClick(v: View?) {
      when(v!!.id){
        R.id.buttonAdd -> {

            viewModel.changeList(filename.text.toString())
            filename.text.clear()
        }
      }
   }
}

Hope this helps.

Post a Comment for "Livedata Not Observing After First Call"