如何从Firebase存储中检索图像并将其放置在RecycleView:android。中,我正在尝试检索上载中的图像!夹

时间:2019-04-10 23:05:17

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

我正在尝试将Firebase存储中的图像放入回收器视图中的应用程序中。与图像有关的文本即将显示,但未显示图像。我不确定我要去哪里错。

我在ImageViewAdapter中使用毕加索来获取图像。

//这是我的imageViewAdapter

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

    private Context context;
    private List<Upload> mupload;

    public ImageAdapter(Context context, List<Upload> uploads){
        this.context = context;
        this.mupload = uploads;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.winners, viewGroup, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
        Upload uploadCurrent = mupload.get(i);
        imageViewHolder.month.setText(uploadCurrent.getMonth());

        Picasso.get().load(uploadCurrent.getImageUri())
               .fit().centerCrop().into(imageViewHolder.imageView);
    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder {

        public TextView month;
        public ImageView imageView;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            month = itemView.findViewById(R.id.tvMonth);
            imageView = itemView.findViewById(R.id.ivWinner);
        }
    }
}

这是我的活动,将在其中显示图像

public class ViewWinnerActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    private ImageAdapter adapter;

    private ProgressBar progressCircle;

    DatabaseReference databaseReference;

    List<Upload> uploads = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_winner);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);

        recyclerView = findViewById(R.id.rv);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));



        progressCircle = findViewById(R.id.progress_circle);

        databaseReference = FirebaseDatabase.getInstance().getReference("Uploads!");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                    Upload upload = postSnapshot.getValue(Upload.class);
                    uploads.add(upload);
                }
                adapter = new ImageAdapter(ViewWinnerActivity.this, uploads);
                recyclerView.setAdapter(adapter);
                progressCircle.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ViewWinnerActivity.this, databaseError.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }

enter image description here

0 个答案:

没有答案