Image Slider(Daimajia)使用Firebase

时间:2017-01-01 09:18:39

标签: java android firebase slider firebase-realtime-database

我试图将滑块上的图片和文字从firebase加载到daimajia滑块,但没有显示任何内容,使用FirebaseRecyclerAdapter,这就是我在下面的方法中所做的: - 。

但我已经宣布

sliderShow = (SliderLayout) findViewById(R.id.slider);
onCreate方法

中的

 public void GetAdverts()
    {


        FirebaseRecyclerAdapter<Advert,MainActivity.EventViewHolder> firebaseRecyclerAdapter_ = new FirebaseRecyclerAdapter<Advert, MainActivity.EventViewHolder>(Advert.class,
                R.layout.slider_image,
                MainActivity.EventViewHolder.class,
                mDatabase) {
            @Override
            protected void populateViewHolder(MainActivity.EventViewHolder viewHolder, final Advert model, final int position) {

            try{


                HashMap<String,String> url_maps = new HashMap<String, String>();
                List<Advert> itemlist = new ArrayList<>();


                url_maps.put(model.getTitle(), model.getImageAdvert());


                for(String name : url_maps.keySet()){
                    TextSliderView textSliderView = new TextSliderView(getApplicationContext());
                    // initialize a SliderLayout
                    textSliderView
                            .description(name)
                            .image(url_maps.get(name));
                    //add your extra information
                    textSliderView.bundle(new Bundle());
                    textSliderView.getBundle()
                            .putString("extra",name);
                    textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                        @Override
                        public void onSliderClick(BaseSliderView slider) {

                            Toast.makeText(getApplicationContext(),
                                    "News Details ",Toast.LENGTH_LONG).show();
                        }
                    });

                    sliderShow.addSlider(textSliderView);
                }

                //   sliderShow.addSlider(textSliderView);
                sliderShow.setPresetTransformer(SliderLayout.Transformer.Accordion);
                sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
                sliderShow.setCustomAnimation(new DescriptionAnimation());
                sliderShow.setDuration(10000);


            }
            catch (Exception e){

                e.printStackTrace();
            }


            }
        };


    }
请问我哪里错了。

我更新的代码。

 void Slider(){

    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, Advert> td = new HashMap<String, Advert>();


            HashMap<String,String> url_maps = new HashMap<String, String>();        

            ArrayList<Advert> values = new ArrayList<>(td.values());

           // List<String> keys = new ArrayList<String>(td.keySet());

            for (Advert ad: values) {

              //  Toast.makeText(getApplicationContext(),""+ad.getImageAdvert(),Toast.LENGTH_LONG).show();
                url_maps.put(ad.getTitle(),ad.getImageAdvert());


            }

            for(String name : url_maps.keySet()){
                TextSliderView textSliderView = new TextSliderView(getApplicationContext());
                // initialize a SliderLayout
                textSliderView
                        .description(name)
                        .image(url_maps.get(name));
                //add your extra information
                textSliderView.bundle(new Bundle());
                textSliderView.getBundle()
                        .putString("extra",name);
                textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                    @Override
                    public void onSliderClick(BaseSliderView slider) {

                        Toast.makeText(getApplicationContext(),
                                "Advertise Here ",Toast.LENGTH_LONG).show();
                    }
                });

                sliderShow.addSlider(textSliderView);
            }

            //   sliderShow.addSlider(textSliderView);
            sliderShow.setPresetTransformer(SliderLayout.Transformer.Accordion);
            sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
            sliderShow.setCustomAnimation(new DescriptionAnimation());
            sliderShow.setDuration(10000);


        }



        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    sliderShow = (SliderLayout) findViewById(R.id.slider);
}

我的广告类。

public class Advert {

public String ImageAdvert;
public String  Title;


public Advert() {

    // left out for firebase ...

}


public String getImageAdvert() {
    return ImageAdvert;
}

public void setImageAdvert(String imageAdvert) {
    ImageAdvert = imageAdvert;
}

public String getTitle() {
    return Title;
}

public void setTitle(String title) {
    Title = title;
}
}

3 个答案:

答案 0 :(得分:1)

FirebaseRecyclerAdapter用于在FirebaseRecycler中设置数据!

如果您只想从DatabaseReference获取数据,则应使用Listeners。

FirebaseDatabse - Retrieving Data

  

要读取路径中的数据并侦听更改,请使用addValueEventListener()oraddListenerForSingleValueEvent()方法将ValueEventListener添加到DatabaseReference。

(儿童还有第三个听众 - ChildEventListener

答案 1 :(得分:0)

我建议您使用两个活动(第一个活动= VoidActivity和第二个活动= MainActivty),VoidActivty从firebase数据库获取数据,然后使用SharedRefrence或intent.putString将其发送到MainActivity

答案 2 :(得分:0)

我建议您使用两个活动(第一个活动= VoidActivity和第二个活动= MainActivty),VoidActivty从firebase数据库检索数据,然后使用SharedPefrence将其发送到MainActivity或使用intent.putString(),它为我运行