我目前无法加载上载到Firebase存储并将URL保存到Firebase数据库的图像。我正在使用Firebase UI和Glide库加载图像。我找到了this tutorial,下面是我的代码:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Students")
.limitToLast(50);
FirebaseRecyclerOptions<Students> options =
new FirebaseRecyclerOptions.Builder<Students>()
.setQuery(query, Students.class)
.build();
FirebaseAdapter = new FirebaseRecyclerAdapter<Students, StudentsHolder>(options) {
@NonNull
@Override
public StudentsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_students, parent, false);
return new StudentsHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull StudentsHolder holder, int position, @NonNull final Students model) {
holder.setName(model.getName());
holder.setPrice(model.getPrice());
holder.setImage(model.getImages(), getApplicationContext());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), model.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
mRecycerView.setAdapter(FirebaseAdapter);
这是我的Student.class:
public class Students {
public String name;
public String price;
public String image;
public Students() {
// Default constructor required for calls to DataSnapshot.getValue(Students.class)
}
public Students(String name, String price, String image) {
this.name = name;
this.price = price;
this.image = image;
}
public String getName() {
return name;
}
public String getPrice() {
return price;
}
public String getImages() {
return image;
}
}
这是StudentsHolder.class:
public class StudentsHolder extends RecyclerView.ViewHolder {
TextView nameTV;
TextView priceTV;
ImageView imageIV;
public StudentsHolder(View itemView) {
super(itemView);
nameTV = itemView.findViewById(R.id.name);
priceTV = itemView.findViewById(R.id.price);
imageIV = itemView.findViewById(R.id.image);
}
public void setName(String studentName) {
nameTV.setText(studentName);
}
public void setPrice(String studentPrice) {
priceTV.setText(studentPrice);
}
public void setImages(String image, Context context) {
StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Students").child(24680).child(image); // this is just an example
Glide.with(context).load(storageReference).into(imageIV);
}
我想知道这是我从Firebase数据库获取图像URL的方式吗?我见过将 addValueEventListener 与 onDataChange 和 onCancelled 结合使用的解决方案,但这有点是在缓慢加载我的数据吗?!