title: How can I refresh the data source of ListAdapter at once after I launch submitList in Android?
body: <p>I use ListAdapter as the source of a RecyclerView, it will display a list of <code>MVoice</code>. The data source is <code>mHomeViewModel.listVoiceBySort</code>, it's <code>LiveData&lt;List&lt;MVoice&gt;&gt;</code>, you can see Code A.</p>
  <p>After I added a record, <code>mHomeViewModel.listVoiceBySort.observe</code> will be launched, <code>myAdapter.submitList(it)</code> will be launched too.</p>
  <p>But I found, the latest <code>mHomeViewModel.listVoiceBySort</code> isn't applied to ListAdapter, you can see Log A.</p>
  <p>How can I refresh the data source of ListAdapter at once after I launch submitList ?</p>
  <p><strong>Log A</strong></p>
  <pre><code>E/My: itemCount: 13 it: 14 CurrentList:13
  </code></pre>
  <p><strong>Code A</strong></p>
  <pre><code>binding.recyclerViewVoice.adapter = myAdapter
  mHomeViewModel.listVoiceBySort.observe(this.viewLifecycleOwner) {
     myAdapter.submitList(it)
     Log.e(&quot;My&quot;,&quot;itemCount: &quot;+myAdapter.itemCount+ &quot; it: &quot;+it.size+ &quot; CurrentList:&quot;+myAdapter.currentList.size)
  
  }
  
  class VoiceAdapters (private val aHomeViewModel: HomeViewModel, private val mPlay: PlayInterface):
          ListAdapter&lt;MVoice, VoiceAdapters.VoiceViewHolder&gt;(MVoiceDiffCallback()) {
  
      private lateinit var mContext: Context
      private lateinit var mLifecycleOwner:LifecycleOwner
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VoiceViewHolder {
          mContext = parent.context
          mLifecycleOwner = mContext as LifecycleOwner
  
          return VoiceViewHolder(
              LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context), parent, false).also {              
                 it.lifecycleOwner = mLifecycleOwner
                 it.aHomeViewModel = aHomeViewModel
              }
          )
      }
  
      override fun onBindViewHolder(holder: VoiceViewHolder, position: Int) {
          val inputMVoice = getItem(position)
          holder.bind(inputMVoice)
      }
  
     
      inner class VoiceViewHolder (private val binding: LayoutVoiceItemBinding):
            RecyclerView.ViewHolder(binding.root) {
         
          fun bind(inputMVoice: MVoice) {                        
              binding.amVoice = inputMVoice          
              binding.executePendingBindings()
          }
      }
  
  }
  
  
  class MVoiceDiffCallback : DiffUtil.ItemCallback&lt;MVoice&gt;() {
      override fun areItemsTheSame(oldItem: MVoice, newItem: MVoice): Boolean {
          return oldItem.id == newItem.id
      }
  
      override fun areContentsTheSame(oldItem: MVoice, newItem: MVoice): Boolean {
          return oldItem == newItem
      }
  }
  </code></pre>
  <p><strong>Added Content</strong></p>
  <p>To Tenfour04: Thanks!</p>
  <p>The following Code B is how to get the LiveDate <code>mHomeViewModel.listVoiceBySort</code>.</p>
  <p><strong>Code B</strong></p>
  <pre><code>class HomeViewModel(val mApplication: Application, private val mDBVoiceRepository: DBVoiceRepository) : AndroidViewModel(mApplication) {
  
      private val _listVoiceBySort=_sortBy.switchMap {
          mDBVoiceRepository.listVoiceBySort(it)
      }
  
      val listVoiceBySort: LiveData&lt;List&lt;MVoice&gt;&gt; =_listVoiceBySort
      ...
  }
  
  class DBVoiceRepository private constructor(private val mDBVoiceDao: DBVoiceDao){
  
      private val _isHaveRecord= MutableLiveData&lt;Boolean&gt;(false)
      val isHaveRecord:LiveData&lt;Boolean&gt; =_isHaveRecord
  
      fun listVoiceBySort(aSortBy: ESortBy): LiveData&lt;List&lt;MVoice&gt;&gt; {
          var query=when (aSortBy){
              ESortBy.StartPriority -&gt;
                 &quot;SELECT * FROM voice_table ORDER BY starred desc, createdDate desc&quot;
              ESortBy.DateDesc -&gt;
                 &quot;SELECT * FROM voice_table ORDER BY createdDate desc&quot;
              ESortBy.DateAsc -&gt;
                 &quot;SELECT * FROM voice_table ORDER BY createdDate&quot;
              ESortBy.TitleDesc -&gt;
                 &quot;SELECT * FROM voice_table ORDER BY title desc&quot;
              ESortBy.TitleAsc -&gt;
                 &quot;SELECT * FROM voice_table ORDER BY title&quot;
          }
          return mDBVoiceDao.runtimeQuery(SimpleSQLiteQuery(query)).map {
              _isHaveRecord.value =!it.isNullOrEmpty()
              it
          }
      }
      ...
  }
  
  @Dao
  interface DBVoiceDao{   
     @RawQuery(observedEntities = [MVoice::class])
     fun  runtimeQuery(sortQuery: SupportSQLiteQuery): LiveData&lt;List&lt;MVoice&gt;&gt;
     ...
  }
  
  
  abstract class DBVoiceDatabase : RoomDatabase() {
     ...
  }
  </code></pre>