Issue
I have a project with matches. In the project, I use the room database. I have three records in my database that have an ID and contain the fields imgTeamOne, imgTeamTwo, titleTeamOne, titleTeamTwo, itemDate. I have prepared a certain activity for each of the match (ActivityDetailOne, ActivityDetailTwo, ActivityDetailThree) and I want that when I clicked on the first card - ActivityDetailOne opened, when I clicked on the second card - ActivityDetailTwo opened, etc. How do I do this? How do I write a code so that a card with ID=0 opens ActivityDetailOne, please help. I'm a beginner and I don't understand
FragmentMatch.kt
class FragmentMatch : Fragment() {
private lateinit var _binding: FragmentMatchBinding
private val binding get() = _binding
private val matchViewModel: ViewModelMatch by activityViewModels()
private lateinit var recyclerView: RecyclerView
private var adapter: RecyclerAdapter? = RecyclerAdapter(emptyList())
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentMatchBinding.inflate(inflater, container, false)
recyclerView = binding.passRecyclerView
recyclerView.layoutManager =
LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
requireActivity().title = "Игры"
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
matchViewModel.matchListLiveData.observe(
viewLifecycleOwner,
Observer { receipts -> receipts?.let { updateUI(receipts) } }
)
}
private fun updateUI(matches: List<MatchModel>) {
if (matches.isEmpty()){
initialDb()
}
adapter = RecyclerAdapter(matches)
recyclerView.adapter = adapter
}
private fun initialDb(){
val match = MatchModel(UUID.randomUUID(), "25 сентября 2021", "Chelsea", "Manchester City")
val match2 = MatchModel(UUID.randomUUID(), "25 сентября 2021", "Everton", "Norvich City")
val match3 = MatchModel(UUID.randomUUID(), "25 сентября 2021", "Brentford", "Liverpool")
matchViewModel.addMatch(match)
matchViewModel.addMatch(match2)
matchViewModel.addMatch(match3)
}
private inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
View.OnClickListener {
private lateinit var match: MatchModel
private val titleTeamOne: TextView = itemView.findViewById(R.id.titleTeamOne)
private val imgTeamOne: ImageView = itemView.findViewById(R.id.imgTeamOne)
private val titleTeamTwo: TextView = itemView.findViewById(R.id.titleTeamTwo)
private val imgTeamTwo: ImageView = itemView.findViewById(R.id.imgTeamTwo)
private val dateGame: TextView = itemView.findViewById(R.id.dateGame)
private val favBtn: Button = itemView.findViewById(R.id.favBtn)
private val btnDetail: Button = itemView.findViewById(R.id.btnDetail)
init {
itemView.setOnClickListener(this)
}
fun bind(match: MatchModel) {
this.match = match
titleTeamOne.text = (match.firstCommand)
titleTeamTwo.text = (match.secondCommand)
dateGame.text = (match.date)
when (match.firstCommand) {
"Everton" -> {
imgTeamOne.setImageResource(R.drawable.image_3)
}
"Brentford" -> {
imgTeamOne.setImageResource(R.drawable.image_4)
}
"Norwich" -> {
imgTeamOne.setImageResource(R.drawable.image_5)
}
"Liverpool" -> {
imgTeamOne.setImageResource(R.drawable.image_6)
}
}
when (match.secondCommand) {
"Everton" -> {
imgTeamTwo.setImageResource(R.drawable.image_3)
}
"Brentford" -> {
imgTeamTwo.setImageResource(R.drawable.image_4)
}
"Norwich" -> {
imgTeamTwo.setImageResource(R.drawable.image_5)
}
"Liverpool" -> {
imgTeamTwo.setImageResource(R.drawable.image_6)
}
}
favBtn.setOnClickListener {
val match = MatchModel(match.id, match.date, match.firstCommand, match.secondCommand, true)
matchViewModel.saveMatch(match)
}
btnDetail.setOnClickListener {
val intent = Intent(context, DetailActivity::class.java)
startActivity(intent)
}
}
override fun onClick(v: View) {
}
}
private inner class RecyclerAdapter(private var matches: List<MatchModel>) :
RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MyViewHolder {
val itemView =
LayoutInflater.from(parent.context)
.inflate(R.layout.item_match, parent, false)
return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val match = matches[position]
holder.bind(match)
}
override fun getItemCount() = matches.size
}
}
Solution
i would open a new field in the room db like "val type: Int" and check the type to open the new activity. But in my opinion you could create only one activity and make it generic for each match
Answered By - Edson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.