ViewPager 返回空数组

ViewPager returning empty array

当我启动我的应用程序时,我希望我的 customSwipeAdapter.java 等到我的 savedImages ArrayList 收到并填充了来自 firebase 的数据。但是我的 class 是 运行 并且我的整个页面是空的,因为 getCount() 方法返回 savedImages.size() 作为 0 因为我的数组列表没有及时填充。当我的数组列表被填充时,对 运行 我的 class 的任何帮助。不知道在这里做什么 :)

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'

我建议您首先在 Activity 上从 Firebase 加载数据,然后将其作为参数传递给适配器的构造函数。这样你的 CustomSwipeAdapter 看起来类似于:

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

关于在 Activity 上从 firebase 加载数据的另一个注意事项:使用带迭代器的 SingleValueListener 而不是 onChildAdded:

mRef.child("Q6i3fI6lNdYYS0z5Jty4WUYE9g13").child("SavedImages").addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                Iterator<DataSnapshot> data = dataSnapshot.getChildren().iterator();
                                while(data.hasNext())
                                {
                                    String savedImage = (String) data.next().child("Image").getValue();
                                    savedImages.add(0, savedImage);
                                }
                                //Data has finished loading. Load your adapter
                                adapter = new customSwipeAdapter(this, savedImages);
                                viewPager.setAdapter(adapter);
                                viewPager.setPageTransformer(false, new DefaultTransformer());
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {}
                        });