Android Firebase retrieve data from all nodes (nes

2019-08-23 07:06发布

问题:

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

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

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

Below are my codes:

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:

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

回答1:

Hello you can make something like this:

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

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