Android Firebase从片段中的所有节点(嵌套)检索数据 [英] Android Firebase retrieve data from all nodes (nested) in Fragments

查看:66
本文介绍了Android Firebase从片段中的所有节点(嵌套)检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用以两种方式检索Firebase数据:

I want my app to retrieve firebase data in 2 ways:

  1. 检索当前登录用户的数据,这是我使用.child(user.getUid())完成的,user = firebaseAuth.getCurrentUser()

  1. Retrieve data for currently logged in user, which I have done using .child(user.getUid()), user = firebaseAuth.getCurrentUser()

从所有用户(包括已登录的用户)中检索数据(因此出现此问题).

Retrieve data from all users including the logged in user (hence this question).

https://i.stack.imgur.com/WnwQg.png

换句话说,我想检索所有圈出的数据值.

In other word, I want to retrieve all the circled data values.

下面是我的代码:

AdminFragment.java:

AdminFragment.java:

public class AdminFragment extends Fragment {

    public AdminFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_admin, container, false);

        final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        final FirebaseUser user = firebaseAuth.getCurrentUser();
        final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerAdmin);
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setReverseLayout(true);
        mLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(mLayoutManager);
        final FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(
            Blog.class,
            R.layout.admin_row,
            BlogViewHolder.class,
            mDatabase
        ) {
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
                viewHolder.setStatus(model.getStatus());
                viewHolder.setPerson(model.getPerson());
                viewHolder.setRequest(model.getRequest());
                viewHolder.setType(model.getType());
                viewHolder.setStart(model.getStart());
                viewHolder.setEnd(model.getEnd());
                viewHolder.setReason(model.getReason());
            }
        };
        recyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver(){
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount){
            super.onItemRangeInserted(positionStart, itemCount);
                int newsCount = recyclerAdapter.getItemCount();
                int lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
                if(lastVisiblePosition == -1 || (positionStart>= (newsCount -1) && lastVisiblePosition == (positionStart -1))){
                    recyclerView.scrollToPosition(positionStart);
                }
            }
        });
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(recyclerAdapter);
        return view;
    }
    private static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mView;
        TextView statusTV, personTV, requestTV, typeTV, startTV, endTV, reasonTV;
        public BlogViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
            statusTV = (TextView) itemView.findViewById(R.id.status);
            personTV = (TextView) itemView.findViewById(R.id.person);
            requestTV = (TextView) itemView.findViewById(R.id.request);
            typeTV = (TextView) itemView.findViewById(R.id.type);
            startTV = (TextView) itemView.findViewById(R.id.start);
            endTV = (TextView) itemView.findViewById(R.id.end);
            reasonTV = (TextView) itemView.findViewById(R.id.reason);
        }
        public void setRequest(String request) {
        requestTV.setText(request);
        }

        void setStatus(String status){
            statusTV.setText(status);
        }

        void setPerson(String person) {
            personTV.setText(person);
        }

        void setType(String type) {
            typeTV.setText(type);
        }

        void setStart(String start) {
            startTV.setText(start);
        }

        void setEnd(String end) {
            endTV.setText(end);
        }

        void setReason(String reason) {
            reasonTV.setText(reason);
        }
    }

}

Blog.java:

Blog.java:

public class Blog {
    private String status, person, request, type, start, end, reason, uid;
    public Blog() {
    }
    public Blog(String status, String person, String request, String type, String start, String end, String reason, String uid) {
        this.status = status;
        this.person = person;
        this.request = request;
        this.type = type;
        this.start = start;
        this.end = end;
        this.reason = reason;
        this.uid = uid;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getPerson() {
        return person;
    }
    public void setPerson(String person) {
        this.person = person;
    }
    public String getRequest() {
        return request;
    }
    public void setRequest(String request) {
        this.request = request;
    }
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getStart() { return start; }
    public void setStart(String start) { this.start = start; }
    public String getEnd() { return end; }
    public void setEnd(String end) { this.end = end; }
    public String getReason() { return reason; }
    public void setReason(String reason) { this.reason = reason; }
    public String getUid() { return uid; }
    public void setUid(String uid) { this.reason = uid; }
}

推荐答案

您好,您可以执行以下操作:

Hello you can make something like this:

Databaseref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot shot : dataSnapshot.getChildren()) {
            }
        }
});

dataSnapshot.getChildren()的每个元素是您节点上具有uid键的用户.

Where each element of dataSnapshot.getChildren() is a user on your node with the key of the uid.

这篇关于Android Firebase从片段中的所有节点(嵌套)检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆