使用firebaseRecyclerAdapter无法加载图像

时间:2017-11-17 17:30:39

标签: android android-studio firebase-realtime-database picasso android-glide

一切正常,但图片无法加载

this image from emulator

this image display the value log of image-getimgUrl

this image frome firebase database

问题是她的图像值和getimgUrl = NULL

公共类MainScreen扩展了AppCompatActivity {

RecyclerView recyclerView;
DatabaseReference mRef;

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


    recyclerView = (RecyclerView)findViewById(R.id.recyclerView_main);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));


    mRef = FirebaseDatabase.getInstance().getReference("Data");

    FirebaseRecyclerAdapter<Model,mViewHolder> adapter =
            new FirebaseRecyclerAdapter<Model, mViewHolder>(Model.class,R.layout.row,mViewHolder.class,mRef) {
        @Override
        protected void populateViewHolder(mViewHolder viewHolder, Model model, int position) {

            viewHolder.setTitle(model.getTitle());
            viewHolder.setImage(getBaseContext(),model.getImgUrl());

            Log.e("getImgUrl", "value : "+model.getImgUrl());
            Log.e("getTitle", "value : "+model.getTitle());
        }
    };

    recyclerView.setAdapter(adapter);
}


public static class mViewHolder extends RecyclerView.ViewHolder{
    View mview;

    public mViewHolder(View itemView) {
        super(itemView);


        mview = itemView;

    }


    public void setTitle(String title){

        TextView postTitle = (TextView) mview.findViewById(R.id.text_row);
        postTitle.setText(title);

        Log.e("title", "value : "+title);
        }
    public void setImage(Context ctx,String image){

        ImageView postImage = (ImageView) mview.findViewById(R.id.image_row);

        Picasso.with(ctx).load(image).into(postImage);

        Log.e("image", "value : "+image);
    }
}

}

2 个答案:

答案 0 :(得分:1)

你的模特错了。试试这个:

public final class Model {
    // Excluding these fields because proguard can make them public
    @Exclude private String mTitle;
    @Exclude private String mImage;

    public Model() {
        // Needed for Firebase
    }

    @Keep
    public String getTitle() {
        return mTitle;
    }

    @Keep
    public void setTitle(String title) {
        mTitle = title;
    }

    @Keep
    public String getImage() {
        return mImage;
    }

    // The "image" part of `setImage` needs to match what it's called in your database.
    // Or you could use `@PropertyName("nameInTheDatabase")`
    @Keep
    public void setImage(String image) {
        mImage = image;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Model model = (Model) o;

        return (mTitle == null ? model.mTitle == null : mTitle.equals(model.mTitle))
                && (mImage == null ? model.mImage == null : mImage.equals(model.mImage));
    }

    @Override
    public int hashCode() {
        int result = mTitle == null ? 0 : mTitle.hashCode();
        result = 31 * result + (mImage == null ? 0 : mImage.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "Model{" +
                "mTitle='" + mTitle + '\'' +
                ", mImage='" + mImage + '\'' +
                '}';
    }
}

答案 1 :(得分:0)

使用Glide 3.7.0版本库而不是Picasso并使用context ApplicationContext intead public void setImage(String imageUrl){ ImageView post_image = (ImageView) mview.findViewById(R.id.imageViewCardRow); Glide.with(mview.getContext).load(imageUrl).into(post_image); }

getProducts(){
    return this.http.get("http://localhost/api/products/select.php")
    .map(res=>{
      this.checkMe = res.json() as any;

      if(this.checkMe._body !== "0"){
         return res.json()
      }

    } );
  }