如何检索存储在Firebase数据库中的图像并显示在“回收者”视图中?

时间:2019-05-12 22:30:29

标签: java android firebase

我目前正在研究使用Firebase的项目。我在使用Firebase时遇到问题。如何检索图像?

ArrayList<Integer> list = new ArrayList<>();
Random random = new Random();

/* Ordered input list */
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

/* Apply random sorting behavior to achieve obtain a shuffled array */
Collections.sort(list, (Integer a, Integer b) -> {
    /* Obtain random number between 0 - 1 */
    Integer randomValue = random.nextInt(1);

    /* Return random positive or negative value which causes "shuffled" 
    sort */
    return randomValue == 1 ? 1 : -1;
});

/* list is now shuffled */

上面的代码显示了Viewholder,而该代码用于通过Viewholder以及模型的getter和setter来执行firebase

public class ViewHolder extends RecyclerView.Adapter<ViewHolder.ImageViewHolder> {
    private  Context mContext;
    private List<Model> mModel;

    public ViewHolder(Context context, List<Model> models)
    {
        mContext=context;
        mModel=models;
    }
    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.row, viewGroup,false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
        Model mModelcur = mModel.get(i);
        imageViewHolder.img_description.setText(mModelcur.getDescription());
        Picasso.get().load(mModelcur.getImage()).into(imageViewHolder.image_view);
    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder
    {
        public TextView img_description;
        public ImageView image_view;
        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            img_description = itemView.findViewById(R.id.rDescription);
            image_view = itemView.findViewById(R.id.rImageview);
        }
    }  
}

该代码有效,但仅显示此recycler part。我已经做了一个像database这样的数据库。如何显示已存储在数据库中的图像并在“回收者”视图中显示?我仍然是使用Firebase的新手,有人可以帮助我吗?

这是我的模型。class@Alex     公共类模型{

public class Ordering extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ViewHolder mAdapter;
    private DatabaseReference mDatabaseReference;
    private List<Model> mModel;

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

        mRecyclerView= findViewById(R.id.recyclerview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mModel = new ArrayList<>();
        mDatabaseReference=FirebaseDatabase.getInstance().getReference("Data");
        mDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
                {
                    Model model=postSnapshot.getValue(Model.class);
                    mModel.add(model);
                }
                mAdapter=new ViewHolder(Ordering.this, mModel);
                mRecyclerView.setAdapter(mAdapter);
            }

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

}

3 个答案:

答案 0 :(得分:0)

override fun onStart() {
    super.onStart()
    mRecyclerView.startListening()
}

override fun onStop() {
    super.onStop()
    mRecyclerView.stopListening()
}

答案 1 :(得分:0)

问题似乎出在您的图像URL中,使用picasso 2.71828,然后您可以看到图像处理的回叫,并且可以看到那里出了什么问题

Picasso.get().load(mModelcur.getImage()).into(imageViewHolder.image_view , new Callback() {
        @Override
        public void onSuccess() {

        }

        @Override
        public void onError(Error e) {
            Log.i("log" , e.getMessage());
        }
    });

答案 2 :(得分:0)

您遇到以下错误:

  

在com.example.firebaseproject.Model类上找不到图像的设置器/字段

因为您正在Model类中使用没有修饰符的文件。要解决此问题,请将修饰符更改为private

private String title, image, description;