Firebase检索实时数据图像不可见,仅显示文本

时间:2019-10-10 22:30:24

标签: android firebase-realtime-database firebase-authentication

我正在尝试将图像从Firebase获取到我的recyclerview,当我从Firebase检索图像和文本时,这是仅显示的文本而不是图像。我已经看到了我的Firebase安全性产品,我允许它从(false)到(true),并且我还通知适配器更改数据,但是它仍然无法正常工作。

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

    private Context mContext;
    private List<Upload> mUploads;


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

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.posts_layout_shape, parent, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {

        Upload uploadCurrent = mUploads.get(position);
        holder.textViewName.setText(uploadCurrent.getName());
        Picasso.with(mContext)
                .load(uploadCurrent.getmImageUrl())
                .placeholder(R.mipmap.logo)
                .fit()
                .centerCrop()
                .into(holder.imageView);

    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public ImageView imageView;

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

            textViewName = itemView.findViewById(R.id.text_view_name);
            imageView = itemView.findViewById(R.id.image_view_upload);

        }
    }

}
public class Fragment_Posts extends Fragment {

    private RecyclerView mRecyclerView;
    private ImageAdapter mAdatper;

    private DatabaseReference mDatabaseRef;
    private List<Upload> mUploads;

    FloatingActionButton newpost;

    View view;


    public Fragment_Posts() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.posts_fragment, container, false);

        mRecyclerView = view.findViewById(R.id.imagerecycler);
        newpost = view.findViewById(R.id.btnnewpost);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mUploads = new ArrayList<>();


        mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

        mDatabaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Upload upload = postSnapshot.getValue(Upload.class);
                    mUploads.add(upload);
                }

                mAdatper = new ImageAdapter(getContext(), mUploads);

                mAdatper.notifyDataSetChanged();

                mRecyclerView.setAdapter(mAdatper);

            }

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

        newpost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), NewPost.class);
                startActivity(intent);
            }
        });


        return view;
    }
}

0 个答案:

没有答案