Я хочу взять данные из API с помощью модернизации и сохранить их в базе данных комнаты.Затем заполнив мой адаптер, я добавил данные в Room, но когда список с данными показывает мне, когда я щелкаю элемент списка пользователей, я щелкаю по X user, и он показывает мне данные для другого пользователя.Я не понял, где моя проблема Вот мой код:
class TutorsFragment : Fragment(), TutorListAdapter.ListItemClickListener, fromBackgroundToMainUIThread {
private var recyclerView: RecyclerView? = null
private var tutorListAdapter: TutorListAdapter? = null
private lateinit var tutors: List<Tutors>
private lateinit var tutorViewModel: StudentTutorViewModel
override fun onTutorsReceived(tutors: List<Tutors>) {
this.tutors = tutors
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val tutorView = inflater.inflate(R.layout.fragment_tutors, null)
recyclerView = tutorView.findViewById<View>(R.id.recyclerview_tutors) as RecyclerView
tutorViewModel = ViewModelProviders.of(this).get(StudentTutorViewModel::class.java)
val layoutManager = LinearLayoutManager(this.activity, LinearLayoutManager.VERTICAL, false)
recyclerView!!.layoutManager = layoutManager
recyclerView!!.setHasFixedSize(true)
tutorViewModel.getAllTutors().observe(viewLifecycleOwner, Observer {
if (it != null) {
tutorListAdapter = TutorListAdapter(it as MutableList<Tutor>?, this@TutorsFragment)
recyclerView!!.adapter = tutorListAdapter
}
})
val api = RetrofitClient.getInstance().apiService
val call = api!!.tutorsList
call.enqueue(object : Callback<List<Tutors>> {
override fun onResponse(call: Call<List<Tutors>>, response: Response<List<Tutors>>) {
Log.d("Tutors: ", response.toString())
val tutorsList = response.body()
tutorsList!!.forEach {
val tutor = Tutor(it.userID!!, it.name!!, it.lastname!!, it.email!!, it.roleFK!!, it.course!!.pathName)
tutorViewModel.addTutor(tutor)
}
if (true) {
this@TutorsFragment.onTutorsReceived(tutorsList)
}
}
override fun onFailure(call: Call<List<Tutors>>, t: Throwable) {
Toast.makeText(activity!!.applicationContext, t.message, Toast.LENGTH_SHORT).show()
}
})
}
val fab = tutorView.findViewById<View>(R.id.fab_tutors) as FloatingActionButton
fab.setOnClickListener {
val fragment = AddStudentAndTutorFragment()
val title = "Add Tutors"
val fragmentManager = fragmentManager
val ft = fragmentManager!!.beginTransaction()
ft.replace(R.id.frame, fragment)
ft.addToBackStack(null)
ft.commit()
(activity as AppCompatActivity).supportActionBar!!.setTitle(title)
}
return tutorView
}
override fun onListItemClick(view: View, position: Int) {
val tutor = tutors[position]
val fragment = UserDetails()
val title = "User Details"
val bundle = Bundle()
bundle.putString("name", tutor.name)
bundle.putString("lastname", tutor.lastname)
bundle.putString("email", tutor.email)
bundle.putInt("userID", tutor.userID!!)
bundle.putInt("role", tutor.roleFK!!)
bundle.putString("course", tutor.course!!.pathName)
fragment.arguments = bundle
val fragmentManager = fragmentManager!!
val ft = fragmentManager.beginTransaction()
ft.replace(R.id.frame, fragment)
ft.addToBackStack(null)
ft.commit()
(activity as AppCompatActivity).supportActionBar!!.title = title
}
override fun onResume() {
super.onResume()
val activity = activity as AppCompatActivity?
val actionBar = activity!!.supportActionBar
val my_fragment_title = "Tutors"
actionBar!!.title = my_fragment_title
}
}
internal interface fromBackgroundToMainUIThread {
fun onTutorsReceived(tutors: List<Tutors>)
}
А вот мой класс адаптера:
class TutorListAdapter(private val tutorsList: MutableList<Tutor>?, private val onItemClickListener: ListItemClickListener) : RecyclerView.Adapter<TutorListAdapter.ListViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewtype: Int): ListViewHolder {
return ListViewHolder(viewGroup.inflate(R.layout.list_item))
}
override fun getItemCount(): Int {
return tutorsList!!.size
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(listViewHolder: ListViewHolder, position: Int) {
val tutors = tutorsList!![position]
listViewHolder.listItemTextView.text = tutors.name + " " + tutors.lastname
}
fun removeItem(position: Int) {
if (position < 0 || position >= tutorsList!!.size) {
return
}
tutorsList.removeAt(position)
notifyItemRemoved(position)
notifyDataSetChanged()
}
interface ListItemClickListener {
fun onListItemClick(view: View, position: Int)
}
inner class ListViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
var listItemTextView: TextView
init {
listItemTextView = view.findViewById<View>(R.id.list_item_textview) as TextView
view.setOnClickListener(this)
}
override fun onClick(v: View) {
onItemClickListener.onListItemClick(v, adapterPosition)
}
}
}
Класс ViewModel:
class StudentTutorViewModel(application: Application) : AndroidViewModel(application) {
var repository: Repository = Repository(application)
fun addTutor(tutor: Tutor) = repository.addTutor(tutor)
fun getAllTutors() = repository.getTutors()
}
Буду признателен, если кто-нибудь сможет мне помочь