this stuff is beating me for hours. I want to change the image on a imageview that is inside a recyclerview from the adapter. The name of the image is coming from firebase (in the field Time from the model) and the image is at resdrawable. Can someone help me or point me to a tutorial? I’m a noobie with kotlin and android stuff.
Main activity
private fun getDataFromFirebase(){
binding.progressBar.visibility = View.VISIBLE
FirebaseDatabase.getInstance("firebaseurl").reference
.get()
.addOnSuccessListener { dataSnapshot->
if(dataSnapshot.exists()){
for (snapshot in dataSnapshot.children){
val quizModel = snapshot.getValue(QuizModel::class.java)
if (quizModel != null) {
quizModelList.add(quizModel)
}
}
}
setupRecyclerView()
}
my adapter
package np.com.bimalkafle.quizonline
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import np.com.bimalkafle.quizonline.databinding.QuizItemRecyclerRowBinding
class QuizListAdapter(private val quizModelList : List<QuizModel>) :
RecyclerView.Adapter<QuizListAdapter.MyViewHolder>() {
class MyViewHolder(private val binding: QuizItemRecyclerRowBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(model : QuizModel){
binding.apply {
quizTitleText.text = model.title
quizSubtitleText.text = model.subtitle
quizTimeText.text = model.time + " min"
quizImage.setImageResource(R.drawable.humanas)
root.setOnClickListener {
val intent = Intent(root.context,QuizActivity::class.java)
QuizActivity.questionModelList = model.questionList
QuizActivity.time = model.time
root.context.startActivity(intent)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding = QuizItemRecyclerRowBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return MyViewHolder(binding)
}
override fun getItemCount(): Int {
return quizModelList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(quizModelList[position])
}
My layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="160dp"
android:src="@drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/rounded_corner"
android:backgroundTint="@color/blueblack"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:letterSpacing="0.1"
android:text="Passei no Encceja"
android:textColor="@color/white"
android:textSize="34sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center"
android:text="Vamos estudar jogando juntos"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="16dp"
android:text="Todas as Disciplinas"
android:textSize="20sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</RelativeLayout>
</LinearLayout>