这个东西让我困扰了好几个小时。我想更改适配器中回收器视图内的图像视图上的图像。图像的名称来自 firebase(在模型的时间字段中),并且图像位于 res\drawable 中。有人可以帮助我或给我指点教程吗?我是 kotlin 和 android 方面的菜鸟。
主要活动
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()
}
我的适配器
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])
}
我的布局
<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>
我发现您在下面调用您的设置回收器视图来获取数据。这其实不是什么好作品。我的建议您可以在不同的调用中初始化您的初始回收器视图。例如,在
oncreate
中调用 init 变量。
在其他地方,在适配器内部,您可以创建一个名为更新数据的新函数。从 firebase 获取数据后调用它。当有新的更改时,运行 recyclerview.notifyDataSetChanged()
来更新或刷新您的回收器视图。