0

I have an application, and in MainFragment there is a recyclesView and the mainAdapter itself, in mainAdapter there is a button when pressed which it should change color (which I implemented), and along with this it should change one item in data base, and I have a problem with calling a function from MainFragment that changes an item in data base, I don't understand how I can call it, if I write MainFragment.updateFavourites, there will be an error that the ViewModel is not initialized, although it is initialized

it MainFragment

class MainFragment : Fragment() {

private lateinit var mUserViewModel: UserViewModel
private val adapter = ListAdapter()

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view =  inflater.inflate(R.layout.fragment_main, container, false)

//        adapter
//        val adapter = ListAdapter() 
//        recyclerView
    val recyclerView = view.recyclerView

    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(requireContext())

  //        UserViewModel
    mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
    mUserViewModel.readAllData.observe(viewLifecycleOwner, Observer { user ->
        adapter.setData(user)
    })

    return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val buttonNewKep: FloatingActionButton = 
 view.findViewById(R.id.floatingActionButtonAddKeep)
    val buttonSearch: ImageView = view.findViewById(R.id.imageSearch)

    buttonNewKep.setOnClickListener {
        Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_addFragment)
    }

    buttonSearch.setOnClickListener {
        Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_searchFragment)
    }
}

  call its Function
fun updateFavourites(id: Int, title: String, subTitle: String, favourites: Boolean) {
    val update = User(id, title, subTitle, favourites)
    mUserViewModel.updateUser(update)
}

}

it mainAdapter

class ListAdapter: RecyclerView.Adapter<ListAdapter.MyViewHolder>() {

private var userList = emptyList<User>()

lateinit var  mainFragment: MainFragment

class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.my_row, parent, false))
}

override fun getItemCount(): Int {
    return userList.size
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val currentItem = userList[position]
    holder.itemView.textTitle.text = currentItem.title
    holder.itemView.textSubTitle.text = currentItem.subTitle


    if(currentItem.favourites) {
        holder.itemView.imageStar.setImageResource(R.drawable.ic_star_icon_select)
    } else {
        holder.itemView.imageStar.setImageResource(R.drawable.ic_star_icon)
    }

    holder.itemView.rowLayout.setOnClickListener {
        val action = MainFragmentDirections.actionMainFragmentToUpdateFragment(currentItem)
        holder.itemView.findNavController().navigate(action)

    }

    holder.itemView.imageStar.setOnClickListener {
        if(currentItem.favourites) {
            holder.itemView.imageStar.setImageResource(R.drawable.ic_star_icon)
             here I have to call the function, and pass there is an id and others, 
          including the modified favorites

        } else {
            holder.itemView.imageStar.setImageResource(R.drawable.ic_star_icon_select)

        }
    }
}

fun setData(user: List<User>){
    this.userList = user
    notifyDataSetChanged()
}

}

  • You need to create interface implement it in your activity it will give you a click event from your adapter in your fragment. [Accepted answer](https://stackoverflow.com/questions/49969278/recyclerview-item-click-listener-the-right-way) has summed it all up. – Malik Saifullah Sep 27 '22 at 19:33

0 Answers0