如何使用Kotlin(包括图像)将数据从firebase检索到回收器视图中?
我正在使用Kotlin将数据从firebase检索到回收器视图中。我能够轻松加载文本,但无法加载图像。我也用过毕加索图书馆。代码中没有错误,有人能提出建议吗 以下是我的代码: 1)这是主要的活动类如何使用Kotlin(包括图像)将数据从firebase检索到回收器视图中?,firebase,kotlin,firebase-realtime-database,android-recyclerview,Firebase,Kotlin,Firebase Realtime Database,Android Recyclerview,我正在使用Kotlin将数据从firebase检索到回收器视图中。我能够轻松加载文本,但无法加载图像。我也用过毕加索图书馆。代码中没有错误,有人能提出建议吗 以下是我的代码: 1)这是主要的活动类 class MainActivity : AppCompatActivity() { lateinit var recyclerView: RecyclerView lateinit var ref:DatabaseReference
class MainActivity : AppCompatActivity()
{
lateinit var recyclerView: RecyclerView
lateinit var ref:DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView=findViewById(R.id.rvFirebase)
ref= FirebaseDatabase.getInstance().reference.child("Restaurants")
recyclerView.layoutManager=LinearLayoutManager(this)
val option=FirebaseRecyclerOptions.Builder<ResModel>().setQuery(ref,ResModel::class.java).build()
val firebaseRecyclerAdapter= object :FirebaseRecyclerAdapter<ResModel,RViewHolder>(option){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RViewHolder {
val itemView= LayoutInflater.from(this@MainActivity).inflate(R.layout.single_row,parent,false)
return RViewHolder(itemView)
}
override fun onBindViewHolder(holder: RViewHolder, position: Int, model: ResModel) {
val refId = getRef(position).key.toString()
ref.child(refId).addValueEventListener(object:ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
holder.rCost.text = model.resCost
holder.rName.text = model.resName
Picasso.get().load(model.resImage).into(holder.rImage)
holder.rRating.text=model.resRating
}
})
}
}
recyclerView.adapter=firebaseRecyclerAdapter
firebaseRecyclerAdapter.startListening()
}
class RViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
var rImage:ImageView=itemView.findViewById(R.id.imgRestaurant)
var rName:TextView=itemView.findViewById(R.id.txtRestaurantName)
var rCost:TextView=itemView.findViewById(R.id.txtCost)
var rRating:TextView=itemView.findViewById(R.id.txtResRating)
}
}
class ResModel
{
var resCost: String =" "
var resImage: String=" "
var resName: String=" "
var resRating: String=" "
constructor():this("","","",""){
}
constructor(resCost:String,resImage: String,resName:String,resRating:String){
this.resCost=resCost
this.resImage=resImage
this.resName=resName
this.resRating=resRating
}
}
3)这是主要的活动xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="@+id/rvFirebase"/>
</RelativeLayout>
不确定毕加索如何处理Firebase ui存储,但使用glide,您可以执行以下操作:
实现'com.firebaseui:firebaseui存储:6.3.0'
添加firebaseui存储
然后:
另外,请查看其他请查看我的问题,因为我已经添加了我如何在firebase中存储数据的图像,并提出了一些建议。
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"
app:cardCornerRadius="4dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/carViewXml">
<LinearLayout
android:id="@+id/llContent"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:background="#ffffff"
android:weightSum="6">
<ImageView
android:layout_marginTop="6dp"
android:layout_weight="1.7"
android:id="@+id/imgRestaurant"
android:layout_width="0dp"
android:layout_height="90dp"
android:src="@drawable/ic_food"
android:padding="5dp"
android:layout_marginStart="10dp"
android:scaleType="centerCrop"
android:fadingEdge="vertical"
android:background="@drawable/rounded_image"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_weight="3.3"
android:layout_marginStart="5dp">
<TextView
android:id="@+id/txtRestaurantName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="name_of_the_restaurant"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/txtCost"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@id/txtRestaurantName"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:padding="8dp"
android:text="Rs. 299"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="9dp"
android:layout_weight="1"
android:layout_marginStart="3dp">
<ImageView
android:id="@+id/imgFavResIcon"
android:layout_width="match_parent"
android:layout_height="23dp"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:src="@drawable/ic_favorite_res" />
<TextView
android:id="@+id/txtResRating"
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_below="@id/imgFavResIcon"
android:layout_marginTop="22dp"
android:padding="4dp"
android:text="4.5"
android:textAlignment="center"
android:textColor="#FB8C00"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
val storageReference: StorageReference = model.resImage //not sure about the reference here - eg FirebaseStorage.getInstance().getReference().child("yourImageReferencePath")
Glide.with(this /* context */)
.using(FirebaseImageLoader())
.load(storageReference)
.into(holder.rImage)