我一直在youtube上关注此tutorial link,有关如何上传和检索图像。一切都进行得很好,直到我到达显示图像的那一部分为止。我的图像没有显示。我没有收到任何错误消息和/或警告消息,但在运行应用程序时只得到了空白页面。下面是我的代码。预先感谢。
//UsersAcivity class
private RecyclerView recyclerView;
private UploadAdapter uploadAdapter;
private DatabaseReference dbReference;
private List<UploadModel> uploads;
FirebaseStorage firebaseStorage;
StorageReference storageR;
private ImageView imageView;
FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
overridePendingTransition(R.anim.slide_right, R.anim.slide_left);
uploads = new ArrayList<>();
mAdapter = new ImageAdapter(getApplicationContext(), uploads);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
uploadAdapter.notifyDataSetChanged();
dbReference = FirebaseDatabase.getInstance().getReference("Users");
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
firebaseStorage = FirebaseStorage.getInstance();
storageR = firebaseStorage.getReference();
firebaseUser = firebaseAuth.getInstance().getCurrentUser();
firebaseDatabase.getInstance().getReference();
dbReference.orderByChild(userid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
UploadModel uploadModel = postsnapshot.getValue(Upload.class);
Uploads.add(uploadModel);
}
uploadAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(UsersActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
//UploadModel class
public class UploadModel {
private String image;
public UploadModel(){
//Empty constructor needed
}
public UploadModel (String imageUrl){
images=imageUrl;
}
public String getImageUrl() {
return image;
}
public void setImageUrl(String image) {
this.image = image;
}
}
UploadAdapter类
public class UploadAdapter extends RecyclerView.Adapter<UploadAdapter.ViewHolder>{
private Context ctext;
private List<Upload> list;
public UploadAdapter(Context context,List<Upload> uploads){
ctext = context;
list = uploads;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(ctext).inflate(R.layout.cardview, parent, false);
return new ViewHolder(V);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent=list.get(position);
Picasso.get().load(uploadCurrent.getImageUrl()).fit().centerCrop().into(holder.imageView);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public View view;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.imageview);
}
}
}
由于您提供的路径,您没有从Firebase Realtime Database得到任何响应。
dbReference.child("LoqCOxqzhpN3puCWwhYDtHJVXqg2").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
UploadModel uploadModel = postsnapshot.getValue(Upload.class);
Uploads.add(uploadModel);
}
uploadAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(UsersActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
您在[用户]下的orderByChild()
查找ID。