使用Firebase数据库填充RecyclerView [英] Populate RecyclerView using Firebase Database

查看:86
本文介绍了使用Firebase数据库填充RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试代码,我想在其中填充一个具有ImageView的RecycleView。
从Firebase数据库中的URL中ImageView将被填充JPG图片。



问题是我没有得到如何检索数据和填充recycleview
我正在读Github上的FirebaseUI,但是我真的很难找,因为我一个人在这个应用程序中。



这是MainActivity代码:

  public class MainActivity extends AppCompatActivity {


DatabaseReference mReference;
DatabaseReference mURLReference;

public FirebaseRecyclerAdapter<图片,图片处理器> mRecyclerViewAdapter;
// UI
RecyclerView mImagesRV;
LinearLayoutManager mManager;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
工具栏工具栏=(工具栏)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


mReference = FirebaseDatabase.getInstance()。getReference();
mURLReference = mReference.child(图片数据);

FloatingActionButton fab =(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){


});

mImagesRV =(RecyclerView)findViewById(R.id.recyclerView);

mManager = new LinearLayoutManager(this);
mManager.setReverseLayout(false);

mImagesRV.setHasFixedSize(false);
mImagesRV.setLayoutManager(mManager);


$ b public void onAttachRecyclerView()
{

查询lastHundred = mURLReference.limitToLast(100);
mRecyclerViewAdapter = new FirebaseRecyclerAdapter< Pictures,PicturesHolder>(
Pictures.class,R.layout.content_main,PicturesHolder.class,lastHundred
){
@Override
保护void populateViewHolder(PicturesHolder pictureView,Pictures pictures,int position){


}
};




$ b $ p $这是我用来插入数据的对象FirebaseDatabase(我只是想要的URL):

pre $ public class Pictures {


字符串pictureID;
字符串downloadURL;
双纬度;
双经度;

public Picture(){

}

public void setPictureID(String pictureID){
this.pictureID = pictureID;
}

public void setDownloadURL(String downloadURL){
this.downloadURL = downloadURL;
}

public void setLatitude(double latitude){
this.latitude = latitude;
}

public void setLongitude(double longitude){
this.longitude = longitude;


public String getPictureID(){
return pictureID;


public String getDownloadURL(){
return downloadURL;
}

public double getLatitude(){
return latitude;
}

public double getLongitude(){
return longitude;




$ b $这是ViewHolder,我不知道如果没关系的话。


  public class PicturesHolder extends RecyclerView.ViewHolder {

ImageView imageView;

public PicturesHolder(查看itemView)
{
super(itemView);
imageView =(ImageView)itemView.findViewById(R.id.picture_view);



public void setImageView(ImageView imageView){
this.imageView = imageView;




$ b pre
$ b $ p $很高兴收到你的好消息伙计们!
非常感谢。

解决方案

我使用Glide在我的RecyclerView中填充图像。给定一个字符串,这是一个HTTP URL,Glide将解析字符串,并显示驻留在该ImageView的HTTP地址的图像。



这里是一个链接到Glide: https://github.com/bumptech/glide



  compile'c​​om.github.bumptech.glide:glide:3.7你需要在你的build.gradle中包含Glide: .0'

在ViewHolder中创建一个使用Glide设置图片的方法setImage():

  public static class PicturesHolder extends RecyclerView.ViewHolder {
Context ctx;
ImageView图像;

public PicturesHolder(View v){
super(v);
this.ctx = v.getContext();
this.image =(ImageView)v.findViewById(R.id.myImageView);

$ b $ public void setImage(String url){
Glide.with(ctx)
.load(url)
.into(image);






然后在populateViewHolder中调用setImage()方法从你的视图的持有者,并作为参数传递getDownloadURL()从您的图片模型的返回值:

$ $ p $ $ $ c $ @Override
protected void populateViewHolder(PicturesHolder viewHolder,Pictures model,int position){
viewHolder.setImage(model.getDownloadURL());
}


Good Evening.

I have a Test code in which I want to populate a RecycleView that has an ImageView. The ImageView will be filled with a JPG image from an URL in Firebase Database.

The problem is that I don't get how to retrieve data and populate recycleview I was reading FirebaseUI on Github but sincerely I find it somehow difficult because I'm alone in this application.

This is the MainActivity code:

public class MainActivity extends AppCompatActivity{


    DatabaseReference mReference;
    DatabaseReference mURLReference;

    public FirebaseRecyclerAdapter<Pictures, PicturesHolder> mRecyclerViewAdapter;
    //UI
    RecyclerView mImagesRV;
    LinearLayoutManager mManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        mReference = FirebaseDatabase.getInstance().getReference();
        mURLReference= mReference.child("Pictures Data");

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        mImagesRV=(RecyclerView)findViewById(R.id.recyclerView);

        mManager=new LinearLayoutManager(this);
        mManager.setReverseLayout(false);

        mImagesRV.setHasFixedSize(false);
        mImagesRV.setLayoutManager(mManager);

    }

    public void onAttachRecyclerView()
    {

        Query lastHundred = mURLReference.limitToLast(100);
        mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Pictures, PicturesHolder>(
                Pictures.class,R.layout.content_main,PicturesHolder.class, lastHundred
        ) {
            @Override
            protected void populateViewHolder(PicturesHolder pictureView, Pictures pictures, int position) {


            }
        };
    }
}

This is the Object that I use to insert data in FirebaseDatabase (I just want the URL):

public class Pictures {


    String pictureID;
    String downloadURL;
    double latitude;
    double longitude;

    public Pictures() {

    }

    public void setPictureID(String pictureID) {
        this.pictureID = pictureID;
    }

    public void setDownloadURL(String downloadURL) {
        this.downloadURL = downloadURL;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getPictureID() {
        return pictureID;
    }

    public String getDownloadURL() {
        return downloadURL;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

And this is the ViewHolder, I don't know if it's okay..

public class PicturesHolder extends RecyclerView.ViewHolder {

    ImageView imageView;

    public PicturesHolder(View itemView)
    {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.picture_view);

    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }
}

It would be nice to receive a good advise from you guys! Thank you so much.

解决方案

I'm using Glide to populate images in my RecyclerView. Given a String that is an HTTP URL, Glide will resolve the String and display the image residing at that HTTP address in your ImageView.

Here's a link to Glide: https://github.com/bumptech/glide

You need to include Glide in your build.gradle:

compile 'com.github.bumptech.glide:glide:3.7.0'

In the ViewHolder create a method setImage() that uses Glide to set the image:

public static class PicturesHolder extends RecyclerView.ViewHolder {
    Context ctx;
    ImageView image;

    public PicturesHolder(View v) {
        super(v);
        this.ctx = v.getContext();
        this.image = (ImageView) v.findViewById(R.id.myImageView);
    }

    public void setImage(String url) {
        Glide.with(ctx)
                .load(url)
                .into(image);
    }
}

Then in populateViewHolder you'll call the setImage() method from your viewholder and pass as an argument the return value of getDownloadURL() from your Pictures model:

@Override
    protected void populateViewHolder(PicturesHolder viewHolder, Pictures model, int position) {
        viewHolder.setImage(model.getDownloadURL());
}

这篇关于使用Firebase数据库填充RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆