LiveData not Observing after first Call

心已入冬 提交于 2019-12-02 10:14:40

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.ConstraintLayout
    xmlns: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">

<Button
        android: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"/>
<EditText
        android: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"/>
<TextView
        android: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.

class MyRepository {

    fun loadFileNames(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

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


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

    fun changeList(filename: String){

       repo.loadFileNames(mutableLiveData, filename)
    }

    fun getFileList() : 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.

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<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)
   }

   override fun onClick(v: View?) {
      when(v!!.id){
        R.id.buttonAdd -> {

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

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!