title: How to select and display a live table with kotlin, room, mvvm?
body: <p>In my First fragment, I create the team name (corresponding to my team_table).</p>
  <p>Then, I move on to the fragment for adding player names. I would like to display directly and only the list of players corresponding to the team. It seems to work when I want to update a player's name and come back to that player list. Otherwise, it shows me the list of all the players of all the teams. How can I do, please?</p>
  <p>Here's my code:</p>
  <pre><code>class UserViewModel(application: Application): AndroidViewModel(application) {
      val readAllDataTeam: LiveData&lt;List&lt;Team&gt;&gt;?
      val readAllDataUser: LiveData&lt;List&lt;User&gt;&gt;?
      val readAllDataEval: LiveData&lt;List&lt;Eval&gt;&gt;?
      val readAllDataUserWithEval: LiveData&lt;List&lt;UserWithEval&gt;&gt;?
      val readAllDataTeamWithUser: LiveData&lt;List&lt;TeamWithUser&gt;&gt;?
  
      private val repositoryTeam: UserRepository
      private val repositoryUser: UserRepository
      private val repositoryEval: UserRepository
      private val repositoryUserWithEval: UserRepository
      private val repositoryTeamWithUser: UserRepository
  
      init {
          val teamDao = UserDatabase.getDatabase(application)?.userDao()
          repositoryTeam = UserRepository(teamDao!!)
          readAllDataTeam = repositoryTeam.readAllDataTeam
  
          val userDao = UserDatabase.getDatabase(application)?.userDao()
          repositoryUser = UserRepository(userDao!!)
          readAllDataUser = repositoryUser.readAllDataUser
  
          val evalDao = UserDatabase.getDatabase(application)?.userDao()
          repositoryEval = UserRepository(evalDao!!)
          readAllDataEval = repositoryEval.readAllDataEval
  
          val userEvalDao = UserDatabase.getDatabase(application)?.userDao()
          repositoryUserWithEval = UserRepository(userEvalDao!!)
          readAllDataUserWithEval= repositoryUserWithEval.readAllDataUserEval
  
          val teamUserDao = UserDatabase.getDatabase(application)?.userDao()
          repositoryTeamWithUser = UserRepository(teamUserDao!!)
          readAllDataTeamWithUser = repositoryTeamWithUser.readAllDataTeamWithUser
      }
  
      //Team
  
      fun addTeam(team: Team) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryTeam.addTeam(team)
          }
      }
  
      fun updateTeam(team: Team) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryTeam.updateTeam(team)
          }
      }
  
      fun deleteTeam(team: Team) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryTeam.deleteTeam(team)
          }
      }
  
      fun deleteAllTeams() {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryTeam.deleteAllTeams()
          }
      }
  
  
      //Eval
  
      fun addEval(eval: Eval) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryEval.addEval(eval)
          }
      }
  
      fun updateEval(eval: Eval) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryEval.updateEval(eval)
          }
      }
  
      fun deleteEval(eval: Eval) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryEval.deleteEval(eval)
          }
      }
  
      fun deleteAllEvals() {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryEval.deleteAllEvals()
          }
      }
  
      //User
  
      fun addUser(user: User) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryUser.addUser(user)
          }
      }
  
      fun updateUser(user: User) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryUser.updateUser(user)
          }
      }
  
      fun deleteUser(user: User) {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryUser.deleteUser(user)
          }
      }
  
      fun deleteAllUsers() {
          viewModelScope.launch(Dispatchers.IO) {
              repositoryUser.deleteAllUsers()
          }
      }
  
      fun searchDatabase(searchQuery: String): LiveData&lt;List&lt;User&gt;&gt;? {
          return repositoryUser.searchDatabase(searchQuery)
      }
      fun searchDatabaseTeam(searchQuery: String): LiveData&lt;List&lt;Team&gt;&gt;? {
          return repositoryUser.searchDatabaseTeam(searchQuery)
      }
  
      fun getTeam(searchTeam: String): LiveData&lt;List&lt;User&gt;&gt;? {
          return repositoryUser.getTeam(searchTeam)
      }
  }
  </code></pre>
  <p><strong>class UserRepository:</strong></p>
  <pre><code>class UserRepository (private val userDao: UserDao) {
      val readAllDataUser: LiveData&lt;List&lt;User&gt;&gt;? = userDao.getUserData()
  
      val readAllDataEval: LiveData&lt;List&lt;Eval&gt;&gt;? = userDao.getEvalData()
  
      val readAllDataTeam: LiveData&lt;List&lt;Team&gt;&gt;? = userDao.getTeamData()
  
      val readAllDataUserEval: LiveData&lt;List&lt;UserWithEval&gt;&gt;? = userDao.getAllUserWithEval()
  
      val readAllDataTeamWithUser: LiveData&lt;List&lt;TeamWithUser&gt;&gt;? = userDao.getAllTeamWithUser()
  
      //User
      suspend fun addUser(user: User) {
          userDao.addUser(user)
      }
  
      suspend fun updateUser(user: User) {
          userDao.updateUser(user)
      }
  
      suspend fun deleteUser(user: User) {
          userDao.deleteUser(user)
      }
  
      suspend fun deleteAllUsers() {
          userDao.deleteAllUsers()
      }
  
      //Eval
      suspend fun addEval(eval: Eval) {
          userDao.addEval(eval)
      }
  
      suspend fun updateEval(eval: Eval) {
          userDao.updateEval(eval)
      }
  
      suspend fun deleteEval(eval: Eval) {
          userDao.deleteEval(eval)
      }
  
      suspend fun deleteAllEvals() {
          userDao.deleteAllEvals()
      }
  
      //Eval
      suspend fun addTeam(team: Team) {
          userDao.addTeam(team)
      }
  
      suspend fun updateTeam(team: Team) {
          userDao.updateTeam(team)
      }
  
      suspend fun deleteTeam(team: Team) {
          userDao.deleteTeam(team)
      }
  
      suspend fun deleteAllTeams() {
          userDao.deleteAllTeams()
      }
  
      fun searchDatabase(searchQuery: String): LiveData&lt;List&lt;User&gt;&gt;? {
          return userDao.searchDatabase(searchQuery)
      }
  
      fun searchDatabaseTeam(searchQuery: String): LiveData&lt;List&lt;Team&gt;&gt;? {
          return userDao.searchDatabaseTeam(searchQuery)
      }
  
      fun getTeam(searchTeam: String): LiveData&lt;List&lt;User&gt;&gt;? {
          return userDao.getTeam(searchTeam)
      }
  </code></pre>
  <p><strong>interface UserDao:</strong></p>
  <pre><code>@Dao
  interface UserDao {
  
      @Query(&quot;SELECT * FROM user_table ORDER BY id ASC&quot;)
      fun getUserData(): LiveData&lt;List&lt;User&gt;&gt;?
  
      @Insert(onConflict = OnConflictStrategy.IGNORE)
      fun addUser(user: User)
  
      @Update
      fun insertEval(eval: Eval)
  
      @Update
      fun updateUser(user: User)
  
      @Delete
      fun deleteUser(user: User)
  
      @Query(&quot;DELETE FROM user_table&quot;)
      fun deleteAllUsers()
  
      @Query(&quot;SELECT * FROM user_table WHERE id=:id&quot;)
      fun getOneUser(id: Int): User
  
      @Transaction
      @Query(&quot;SELECT * FROM user_table JOIN eval_table  on user_table.id = eval_table.idEval &quot;)
      fun getAllUserWithEval(): LiveData&lt;List&lt;UserWithEval&gt;&gt;
  
      @Query(&quot;SELECT * FROM user_table WHERE firstName LIKE :searchQuery OR lastName LIKE :searchQuery OR nbTeam LIKE :searchQuery OR nameTeam LIKE :searchQuery&quot;)
      fun searchDatabase(searchQuery: String): LiveData&lt;List&lt;User&gt;&gt;?
  
      @Query(&quot;SELECT * FROM team_table WHERE  nameTeam LIKE :searchQuery&quot;)
      fun searchDatabaseTeam(searchQuery: String): LiveData&lt;List&lt;Team&gt;&gt;?
  
      @Transaction
      @Query(&quot;SELECT * FROM user_table WHERE nameTeam LIKE :searchTeam&quot;)
      fun getTeam(searchTeam: String): LiveData&lt;List&lt;User&gt;&gt;?
  </code></pre>
  <p><strong>class ListFragment:</strong></p>
  <pre><code>class ListFragment : Fragment(), SearchView.OnQueryTextListener {
  
      //Pour accéder au fab
      private var _binding: FragmentListBinding? = null
      private val binding get() = _binding!!
      private lateinit var mUserViewModel: UserViewModel
      private val args by navArgs&lt;ListFragmentArgs&gt;()
  
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          _binding = FragmentListBinding.inflate(inflater, container,false)
          val view = binding.root
          //Recyclerview pour affichier le custom row et les données
          val adapter = ListAdapter()
          val recyclerView = view.findViewById&lt;RecyclerView&gt;(R.id.recyclerview)
          recyclerView?.adapter = adapter
          recyclerView?.layoutManager = LinearLayoutManager(requireContext())
  
          val searchTeam = args.currentTeam.nameTeam
  
          binding.tvNameTeam.text = searchTeam
  
          // UserViewModel pour afficher les données
          mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
  
  
          mUserViewModel.getTeam(searchTeam)?.observe(viewLifecycleOwner, Observer {
              userT -&gt; adapter.setData(userT)
          })
          /*
          mUserViewModel.getTeam(args.currentTeam.nameTeam)
              ?.observe(viewLifecycleOwner, Observer { userT -&gt;
                  adapter.setData(userT)
              })
  
           */
  
          binding.floatingActionButton.setOnClickListener{
              findNavController().navigate(R.id.action_listFragment_to_addFragment)
          }
  
          // Add menu
          setHasOptionsMenu(true)
          return view
      }
  
      override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
          inflater.inflate(R.menu.main_menu, menu)
          inflater.inflate(R.menu.delete_menu, menu)
          val search = menu?.findItem(R.id.menu_search)
          val searchView = search?.actionView as? SearchView
          searchView?.isSubmitButtonEnabled = true
          searchView?.setOnQueryTextListener(this)
          return
      }
  
      override fun onOptionsItemSelected(item: MenuItem): Boolean {
          if (item.itemId == R.id.menu_delete){
              deleteAllUsers()
          }
          return super.onOptionsItemSelected(item)
      }
  
      override fun onDestroyView() {
          super.onDestroyView()
          _binding = null
      }
  
      private fun deleteAllUsers() {
          val builder = AlertDialog.Builder(requireContext())
          builder.setPositiveButton(&quot;Yes&quot;){ _, _ -&gt;
              mUserViewModel.deleteAllUsers()
              Toast.makeText(
                  requireContext(),
                  &quot;Suppression complète réussie&quot;,
                  Toast.LENGTH_SHORT).show()
          }
          builder.setNegativeButton(&quot;No&quot;){_, _ -&gt; }
          builder.setTitle(&quot;Tout Supprimer?&quot;)
          builder.setMessage(&quot;Êtes-vous sûr de vouloir supprimer l'ensemble des données?&quot;)
          builder.create().show()
      }
  
      override fun onQueryTextSubmit(query: String?): Boolean {
          return true
      }
  
      override fun onQueryTextChange(query: String?): Boolean {
          if(query != null){
              searchDatabase(query)
          }
          return true
      }
  
      private fun searchDatabase(query: String) {
          val searchQuery = &quot;%$query%&quot;
  
          val adapter = ListAdapter()
          val recyclerView = view?.findViewById&lt;RecyclerView&gt;(R.id.recyclerview)
          recyclerView?.adapter = adapter
          recyclerView?.layoutManager = LinearLayoutManager(requireContext())
          mUserViewModel.searchDatabase(searchQuery)?.observe(this, { userT-&gt;
              userT.let {
                  adapter.setData(it)
              }
          })
      }
  }
  </code></pre>
  <p><strong>class ListAdapter:</strong></p>
  <pre><code>class ListAdapter : RecyclerView.Adapter&lt;ListAdapter.MyViewHolder&gt;() {
  
      var userTList = emptyList&lt;User&gt;()
  
      class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
          return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_row, parent, false))
      }
  
      override fun getItemCount(): Int {
          return userTList.size
      }
  
      override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
          val currentItemUT = userTList[position]
  
          holder.itemView.findViewById&lt;TextView&gt;(R.id.id_txt).text = currentItemUT.id.toString()
          holder.itemView.findViewById&lt;TextView&gt;(R.id.firstName_txt).text = currentItemUT.firstName
          holder.itemView.findViewById&lt;TextView&gt;(R.id.lastName_txt).text = currentItemUT.lastName
          holder.itemView.findViewById&lt;TextView&gt;(R.id.numeroTeam_txt).text = currentItemUT.nbTeam.toString()
  
          holder.itemView.findViewById&lt;View&gt;(R.id.rowLayout).setOnClickListener {
              val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItemUT)
              holder.itemView.findNavController().navigate(action)
          }
  
          holder.itemView.findViewById&lt;Button&gt;(R.id.btn_start_eval).setOnClickListener {
              val action = ListFragmentDirections.actionListFragmentToEvalFragment(currentItemUT)
              holder.itemView.findNavController().navigate(action)
          }
  
      }
  
      fun setData(user: List&lt;User&gt;){
          this.userTList = user
          notifyDataSetChanged()
      }
  
  }
  </code></pre>