将图像从Firebase存储中检索到recyclerview

时间:2017-04-06 07:28:58

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

如何通过使用Picasso在我的Firebase存储中获取其URL来检索图像,以便将其加载到recyclerview holder中。我在毕加索看到了这个,但我不知道如何实现它。
Picasso.with(context).load(" http://i.imgur.com/DvpvklR.png")。into(aImage); 我也在四处搜索,但我看到的样本却完全不同。

以下是代码段:

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;
import .......

public class FragSearch extends Fragment {

RecyclerView mRecyclerView;
private DatabaseReference mDatabaseReference;
private LinearLayoutManager mLinearLayoutManager;

private StorageReference mStorageRef;
private FirebaseRecyclerAdapter<MsgPack, MessageViewHolder> mFirebaseAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.up_msg, container, false);

    mRecyclerView=(RecyclerView)view.findViewById(R.id.recycleNView);
    mRecyclerView.setHasFixedSize(false);
    mLinearLayoutManager=new LinearLayoutManager(getContext());

    return view;
}

@Override
public void onStart() {
    super.onStart();
    mStorageRef = FirebaseStorage.getInstance().getReference();
    mDatabaseReference= FirebaseDatabase.getInstance().getReference();
    mFirebaseAdapter=new FirebaseRecyclerAdapter<MsgPack, MessageViewHolder>(
            MsgPack.class,
            R.layout.post_listing,
            MessageViewHolder.class,
            mDatabaseReference.child("stackpack"))
    {

        @Override
        protected void populateViewHolder(final MessageViewHolder viewHolder, MsgPack model, int position) {

            viewHolder.bText.setText(model.getTopic());
            viewHolder.dText.setText(model.getImageurl());

        }
    };

    mRecyclerView.setAdapter(mFirebaseAdapter);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);
}

@Override
public void onResume() {
    super.onResume();
}

public static class MessageViewHolder extends RecyclerView.ViewHolder{
        public TextView aText,bText,cText;
    View mView;
    LinearLayout pack;
    ImageView aImage;

    public MessageViewHolder(View v) {
        super(v);
        mView=v;


        bText = (TextView) mView.findViewById(R.id.txtTopic);
        aImage=(ImageView)mView.findViewById(R.id.imageView);

        /*How do I load retrieve the images via url in my firebase storage using picasso
       */ Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(aImage);
    }
}
}

json格式

{"stackpack" : {
  "imageurl" : "http://firebase.........er23...",
  "topic" : "THANKS"
},   
}

msgPack.class

.....
public class MsgPack {

private String topic;
private String imageurl;
public MsgPack(){
}
public MsgPackString topic, String imageurl) {   
    this.topic = topic;
    this.imageurl=imageurl;

}

public String getTopic() {
    return topic;
}
public String getImageurl() {
    return imageurl;
}
public void setTopic(String topic) {
    this.topic = topic;
}
public void setImageurl(String imageurl) {
    this.imageurl = imageurl;
}
}

我只是想从实时firebase获取imageurl(这是指向firebase存储中的资源的字符串)值,并且我的imageview加载了该图像 - 使用picasso

1 个答案:

答案 0 :(得分:1)

您可以在populateViewHolder方法上使用它。只需从ImageView中获取上下文:

@Override
        protected void populateViewHolder(final MessageViewHolder viewHolder, MsgPack model, int position) {

            viewHolder.bText.setText(model.getTopic());
            viewHolder.dText.setText(model.getImageurl());
            Picasso.with(viewHolder.aImage.getContext()).load(model.getImageurl()).into(viewHolder.aImage);

        }