我正在尝试从Firebase到我的“回收”视图中检索图像和文本,这很好用。我遇到的唯一问题是,如果我使用其他手机登录,则不会加载图像,但是会加载文本数据。任何帮助将不胜感激。
从Firebase获取数据
//Retrieves information stored inside Post node...
public void fetchUserInfo() {
postRef = FirebaseDatabase.getInstance().getReference().child("Post");
postRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
value = ds.getValue(Post.class);
postList.add(value);
}
adapter = new Adapter(Shop_Activity.this, postList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("Error", databaseError.toString());
}
});
}
}
通过这种方法,我将必要的信息保存在字典中,并将其上传到Firebase
//saves user image and description inside firebase
public void saveToFirebase(){
String userId = mAuth.getCurrentUser().getUid();
postDictionary.put("desc", descriptionEditText.getText().toString());
postDictionary.put("image", selectedImageUri.toString());
postDictionary.put( "id",userId);
productsDatabaseRef.child("Post").push().setValue(postDictionary);
Intent intent = new Intent(Upload_Post.this, Shop_Activity.class);
startActivity(intent);
}
适配器
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
ArrayList<Post> userPost;
public Adapter(Context context, ArrayList<Post> userPost){
this.context = context;
this.userPost = userPost;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.shop_layout_design,viewGroup, false));
}
//this is where you set the value for the ui elements
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.desc.setText(userPost.get(i).getdesc());
Glide.with(this.context).load(userPost.get(i).getimage()).into(viewHolder.image);
//Picasso.get().load(userPost.get(i).getimage()).into(viewHolder.image);
}
@Override
public int getItemCount() {
return userPost.size();
}
//links up ui elements
class ViewHolder extends RecyclerView.ViewHolder{
TextView desc;
TextView id;
ImageView image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
id = itemView.findViewById(R.id.post_title);
desc = itemView.findViewById(R.id.post_desc);
image = itemView.findViewById(R.id.post_image);
}
}
}