ViewPager返回空数组

时间:2017-08-31 19:37:32

标签: firebase arraylist firebase-realtime-database android-viewpager android-pageradapter

当我启动我的应用时,我希望我的customSwipeAdapter.java等到我的savedImages ArrayList收到并填充了来自firebase的数据。但是我的课程正在运行而我的整个页面都是空的,因为getCount()方法将savedImages.size()返回为0,因为我的arraylist没有及时填充。在填充我的数组列表时,可能有任何关于运行我的类的帮助。不知道该怎么做:)

customSwipeAdapter.java

    public class customSwipeAdapter extends PagerAdapter {

            private Firebase mRef;

            private Context ctx;
            private LayoutInflater layoutInflator;
            public customSwipeAdapter(Context ctx) {
                this.ctx = ctx;
            }

            private int[] frontImages = {R.drawable.amen_parham, R.drawable.janel_parham, R.drawable.kevin_parham};

            // Populate ArrayList with firebase data
            List<String> savedImages = new ArrayList<String>();

            Boolean goingToCallOnce = false;
            Boolean finishedLoadingData = false;

            @Override
            public int getCount() {
                return savedImages.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object o) {
                return (view == o);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                getSavedImages_FromDB();

                layoutInflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View item_view = layoutInflator.inflate(R.layout.swipe_layout, container, false);

                final EasyFlipView mYourFlipView = (EasyFlipView) item_view.findViewById(R.id.flipView);

                ImageView imageView_Front = (ImageView) item_view.findViewById(R.id.imageView_Front);
                imageView_Front.setImageResource(frontImages[position]);

                container.addView(item_view);

                System.out.println(savedImages);

                return item_view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((RelativeLayout)object);
            }

            public void getSavedImages_FromDB() {
                mRef = new Firebase("");

                if (goingToCallOnce == false) {
                   goingToCallOnce = true;
                mRef.child("Q6i3fI6lNdYYS0z5Jty4WUYE9g13").child("SavedImages").addChildEventListener(new ChildEventListener() {

                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    String savedImage = (String) dataSnapshot.child("Image").getValue();

                        savedImages.add(0, savedImage);

                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }

        mRef.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                finishedLoadingData = true;
                System.out.println("finishedLoadingData");
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }
}

savedCardsViewController.java

public class savedCardsViewController extends AppCompatActivity {

    private Swipe swipe;
    ViewPager viewPager;
    customSwipeAdapter adapter;

    private Firebase mRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saved_cards_view_controller);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        adapter = new customSwipeAdapter(this);
        viewPager.setAdapter(adapter);
        viewPager.setPageTransformer(false, new DefaultTransformer());

    }
}

'Context' to 'ValueEventListener'

2'nd 'Context' to 'ValueEventListener'

1 个答案:

答案 0 :(得分:0)

我建议您先在Activity上加载Firebase中的数据,然后将其作为参数传递给适配器的构造函数。这样您的THREE.texture看起来就像这样:

CustomSwipeAdapter

关于在Activity上从firebase加载数据的另一个注意事项:使用带有迭代器而不是public class customSwipeAdapter extends PagerAdapter { private Firebase mRef; private Context ctx; private LayoutInflater layoutInflator; List<String> savedImages = new ArrayList<String>(); public customSwipeAdapter(Context ctx, List<String> savedImages){ this.ctx = ctx; this.savedImages = savedImages } ... } 的SingleValueListener:

onChildAdded