使用Firebase数据库填充RecyclerView

时间:2016-12-04 06:30:36

标签: java android firebase firebase-realtime-database

晚安。

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

问题在于我无法获取如何检索数据和填充recycleview 我正在Github上阅读FirebaseUI,但我真的觉得它很难,因为我在这个应用程序中独自一人。

这是MainActivity代码:

Set Fcell = formulaWks.Range("O7")

'formula = "=$A1+$B1"  ' example for testing
Formula = "=IF(YEAR(NOW())=$W$3,IF(ISBLANK($G7),"",IFERROR(IF(DATEDIF(TODAY(),$N7,"y")=0,"",DATEDIF(TODAY(),$N7,"y")&" y ")&IF(DATEDIF(TODAY(),$N7,"ym")=0,"",DATEDIF(TODAY(),$N7,"ym")&" m ")&IF(DATEDIF(TODAY(),$N7,"md")=0,"",DATEDIF(TODAY(),$N7,"md")&" d"),"wrong date")),"Package completed")"
Fcell = ActiveCell.formula

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

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) {


            }
        };
    }
}

这是ViewHolder,我不知道它是否可以。

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;
    }
}

很高兴收到你们的好意见! 非常感谢你。

1 个答案:

答案 0 :(得分:1)

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

这里是Glide的链接:https://github.com/bumptech/glide

您需要在build.gradle中包含Glide:

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

在ViewHolder中创建一个方法setImage(),它使用Glide来设置图像:

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);
    }
}

然后在populateViewHolder中,您将从viewholder调用setImage()方法,并从Pictures模型中将getDownloadURL()的返回值作为参数传递:

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