Firebase图像未在recyclerview片段中显示

时间:2020-04-25 10:27:37

标签: javascript android android-recyclerview

我正在尝试从数据库检索数据到recyclerview,但是图像的唯一名称显示在recyclerview中。图像未在image中加载。这是我的代码和Firebase图片。 fragmentHealthtips.java

package com.example.bbeast.HomeActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.bbeast.R;
import com.example.bbeast.Upload;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class fragmentHealthTips extends Fragment {
    private RecyclerView mRecyclerView;
    private DatabaseReference mDataRef;




    public static class hViewHolder extends RecyclerView.ViewHolder{
        TextView uName;
        ImageView hImageView;

        public hViewHolder(@NonNull View itemView) {
            super(itemView);
            uName = itemView.findViewById(R.id.healthtips_name);
            hImageView = itemView.findViewById(R.id.healthtips_imageView);

        }
    }


    View view;

    public void fragmentHealthTips() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.healthtips_fragment, container, false);
        mRecyclerView =(RecyclerView)view.findViewById(R.id.healthtips_recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataRef = FirebaseDatabase.getInstance().getReference().child("Uploads");
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerOptions options =
                new FirebaseRecyclerOptions.Builder<HealthTips>()
                .setQuery(mDataRef, HealthTips.class)
                .build();
        FirebaseRecyclerAdapter<HealthTips, hViewHolder> adapter= new FirebaseRecyclerAdapter<HealthTips, hViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull final hViewHolder holder, int i, @NonNull final HealthTips healthTips) {
                String hName = healthTips.getName();
                String hImage = healthTips.getImageUrl();
                holder.uName.setText(hName);
                Picasso.get().load(hImage).into(holder.hImageView);



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

        mRecyclerView.setAdapter(adapter);
        adapter.startListening();

    }


}

Healthtips.java(模型类)

package com.example.bbeast.HomeActivity;

import java.util.jar.Attributes;

public class HealthTips {
    private String hName, hImageUrl;

    public HealthTips(){


    }
    public HealthTips(String name, String imageUrl){

        hName = name;
        hImageUrl = imageUrl;

    }
    public String getName(){
        return hName;
    }
    public void setName(String name){
        hName = name;
    }
    public String getImageUrl(){
        return hImageUrl;
    }
    public void setImageUrl(String imageUrl){
        hImageUrl= imageUrl;
    }


}

变量也与数据库中的变量相同,读取的值为true。这是数据库的图像。

enter image description here

enter image description here

如果我的代码有问题,请告诉我。正在显示图像的名称,但未在recyclerview中加载图像。预先谢谢你。

0 个答案:

没有答案