Recycleview并从Firebase获取数据

时间:2019-08-22 17:27:39

标签: android

我需要从Firebase调用数据。并在此代码中设置。 我不明白如何从Firebase获取数据。 我想通过从Firebase获取数据来修改此示例。 请任何人指导我。这对我很重要。

https://androidwave.com/exoplayer-in-recyclerview-in-android/

private void prepareVideoList() {
MediaObject mediaObject = new MediaObject();
mediaObject.setId(1);
mediaObject.setViews(200);
mediaObject.setLlke(100);
mediaObject.setDislike(100);
mediaObject.setUserHandle("@h.pandya");
mediaObject.setTitle(
        "Do you think the concept of marriage will no longer exist in the future?");
mediaObject.setCoverUrl(
        "https://androidwave.com/media/images/exo-player-in-recyclerview-in-android-1.png");
mediaObject.setUrl("https://androidwave.com/media/androidwave-video-1.mp4");

MediaObject mediaObject2 = new MediaObject();
mediaObject2.setId(2);
mediaObject2.setViews(200);
mediaObject2.setLlke(100);
mediaObject2.setDislike(100);
mediaObject2.setUserHandle("@hardik.patel");
mediaObject2.setTitle(
        "If my future husband doesn't cook food as good as my mother should I scold him?");
mediaObject2.setCoverUrl(
        "https://androidwave.com/media/images/exo-player-in-recyclerview-in-android-2.png");
mediaObject2.setUrl("https://androidwave.com/media/androidwave-video-2.mp4");

MediaObject mediaObject3 = new MediaObject();
mediaObject3.setId(3);
mediaObject3.setViews(200);
mediaObject3.setLlke(100);
mediaObject3.setDislike(100);
mediaObject3.setUserHandle("@arun.gandhi");
mediaObject3.setTitle("Give your opinion about the Ayodhya temple controversy.");
mediaObject3.setCoverUrl(
        "https://androidwave.com/media/images/exo-player-in-recyclerview-in-android-3.png");
mediaObject3.setUrl("https://androidwave.com/media/androidwave-video-3.mp4");

MediaObject mediaObject4 = new MediaObject();
mediaObject4.setId(4);
mediaObject4.setViews(200);
mediaObject4.setLlke(100);
mediaObject4.setDislike(100);
mediaObject4.setUserHandle("@sachin.patel");
mediaObject4.setTitle("When did kama founders find sex offensive to Indian traditions");
mediaObject4.setCoverUrl(
        "https://androidwave.com/media/images/exo-player-in-recyclerview-in-android-4.png");
mediaObject4.setUrl("https://androidwave.com/media/androidwave-video-6.mp4");

MediaObject mediaObject5 = new MediaObject();
mediaObject5.setId(5);
mediaObject5.setViews(200);
mediaObject5.setLlke(100);
mediaObject5.setDislike(100);
mediaObject5.setUserHandle("@monika.sharma");
mediaObject5.setTitle("When did you last cry in front of someone?");
mediaObject5.setCoverUrl(
        "https://androidwave.com/media/images/exo-player-in-recyclerview-in-android-5.png");
mediaObject5.setUrl("https://androidwave.com/media/androidwave-video-5.mp4");

mediaObjectList.add(mediaObject);
mediaObjectList.add(mediaObject2);
mediaObjectList.add(mediaObject3);
mediaObjectList.add(mediaObject4);
mediaObjectList.add(mediaObject5);
mediaObjectList.add(mediaObject);
mediaObjectList.add(mediaObject2);
mediaObjectList.add(mediaObject3);
mediaObjectList.add(mediaObject4);
mediaObjectList.add(mediaObject5);

}

我尝试使用,但应用水芹

reference = FirebaseDatabase.getInstance().getReference().child("Stories");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mediaObjectList = new ArrayList<MediaObject>();
            for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
            {
                MediaObject p = dataSnapshot1.getValue(MediaObject.class);
                mediaObjectList.add(p);
            }
            mRecyclerView.setMediaObjects(mediaObjectList);
            mAdapter = new MediaRecyclerAdapter(mediaObjectList, initGlide());

            //Set Adapter
            mRecyclerView.setAdapter(mAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
           // Toast.makeText(MainActivity.this, "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
        }
    });

0 个答案:

没有答案