如何从Firebase数据库检索阵列数据?

时间:2019-07-05 07:09:44

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

enter image description here

我从recylerview的firebase数据库中检索了数据。但是我在发布节点“ postimg”中有另一个列表项。

为此,我在onBindViewHolder中使用了另一个侦听器。但这对我来说似乎不正确。

那么如何在mainactivity中完成所有这些工作并传递适配器?

这是我的代码:

@Override
    public void onBindViewHolder(@NonNull final PostHolder postHolder, final int i) {
        postHolder.setData(mPost.get(i));
        final String PostKey=mPost.get(i).getPostid();
        FirebaseAuth mAuth=FirebaseAuth.getInstance();
        final String currentUserID=mAuth.getCurrentUser().getUid();
        final DatabaseReference post=FirebaseDatabase.getInstance().getReference().child("Posts");
post.child(PostKey).child("postimg").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                if (dataSnapshot.exists())
                {
                    for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
                    {
                        String postimagelink =dataSnapshot1.getValue().toString();
                        postimagelist.add(postimagelink);
                    }


                    String[] urls =postimagelist.toArray(new String[postimagelist.size()]);
                    postHolder.mPager.setAdapter(new SlidingImage_Adapter(mContext,urls));

                    postHolder.indicator.setViewPager(postHolder.mPager);

                    final float density = mContext.getResources().getDisplayMetrics().density;
                    postHolder.indicator.setRadius(5 * density);

                    postHolder.NUM_PAGES = urls.length;



                    postHolder.indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
                    {

                        @Override
                        public void onPageSelected(int position) {
                            postHolder.currentPage = position;
                        }

                        @Override
                        public void onPageScrolled(int pos, float arg1, int arg2) {
                        }

                        @Override
                        public void onPageScrollStateChanged(int pos) {
                        }
                    });
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

这是我在MainActivity中的查询。

mAdapter = new PostAdapter(MainActivity.this);
query = PostRef
                    .orderByChild("timestamp")
                    .limitToLast(mPostsPerPage);
query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                List<Post> userModels = new ArrayList<>();


                for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                    String o=userSnapshot.getKey();
                    userModels.add(userSnapshot.getValue(Post.class));
//Here I want to retrieve no of "postimg"child and its value and pass in adapter//

                }

                mAdapter.addAll(userModels);

            }

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

0 个答案:

没有答案