图像未在reyclerview中显示,图像来自Firebase

时间:2020-06-28 15:55:31

标签: java android firebase firebase-realtime-database android-recyclerview

我正在制作一个应用程序,其中图像将显示在recylerview中,并且所有图像都将从firebase中感染。但是,图像未在reyclerview中显示。我是Android新手,找不到问题,请帮帮我。

这是我的代码:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    binding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_home, container, false);
    View view = binding.getRoot();
    databaseImage = FirebaseDatabase.getInstance().getReference("TopApps");
    valueEventListener =databaseImage.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            modelClasses.clear();
            for(DataSnapshot item : dataSnapshot.getChildren()){
                String mImageURL =item.child("imageUrl").getValue().toString();
                String mImageLink = item.child("webUrl").getValue().toString();
                ModelClass modelClass = new ModelClass(mImageURL, mImageLink);
                modelClasses.add(modelClass);
            }
            adapter = new TopRecyclerAdapter(modelClasses, getContext());
            binding.toprecyclerview.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

MyAdapter代码:

public class TopRecyclerAdapter extends RecyclerView.Adapter<TopRecyclerAdapter.ViewHolder> {
ArrayList<ModelClass>modelClasses;
Context context;

public TopRecyclerAdapter(ArrayList<ModelClass> modelClasses, Context context) {
    this.modelClasses = modelClasses;
    this.context = context;
}

@NonNull
@Override
public TopRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.topapps_layout, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull TopRecyclerAdapter.ViewHolder holder, int position) {
    Picasso.get().load(modelClasses.get(position).getImageUrl()).into(holder.imageView);

}

@Override
public int getItemCount() {
    return modelClasses.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    CircleImageView imageView;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.circleimg);
    }
}

}

ModelClass:

public class ModelClass {
String imageUrl;
String imageLink;

public ModelClass(String imageUrl, String imageLink) {
    this.imageUrl = imageUrl;
    this.imageLink = imageLink;
}

public String getImageUrl() {
    return imageUrl;
}

public String getImageLink() {
    return imageLink;
}

}

数据库结构:

enter image description here

请告诉我问题是什么。为什么图像没有在recyclerview中显示。

1 个答案:

答案 0 :(得分:2)

//这些规则将允许任何人对您的数据库进行读写访问

{
  "rules": {
    ".read": true,
    ".write": true
  }
}