从Firebase中检索用户帖子位于uid

时间:2018-12-04 11:04:58

标签: android firebase firebase-realtime-database

我有一个Android应用,用户可以在其中提交帖子,只有他们可以查看自己发布的内容。我使用下面的代码实现了

 String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    mDatabase = FirebaseDatabase.getInstance().getReference().child("reports").child(uid);

但是,我有一个单独的管理应用程序,我想查看所有用户发布的内容,但我无法实现。请帮助

下面是我的数据库的图像 Database image

3 个答案:

答案 0 :(得分:0)

mDatabase = FirebaseDatabase.getInstance().getReference().child("reports")将为您获取所有报告。

答案 1 :(得分:0)

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference reportsRef = rootRef.child("reports");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String, Object> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String docId = ds.getKey();
            list.add(docId);
            Log.d("TAG", docId);
        }
        Log.d("TAG", list);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
reportsRef.addListenerForSingleValueEvent(eventListener);

答案 2 :(得分:0)

这是我的代码

public class MyReports extends Fragment {


private RecyclerView mBlogList;
private GridLayoutManager gridLayoutManager;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseUsers;

private DatabaseReference mDatabaseLike;

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListner;

private boolean mProcessLike = false;


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


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

    mAuth =FirebaseAuth.getInstance();

    mAuthListner = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            if(firebaseAuth.getCurrentUser() == null){

                Intent loginIntent = new Intent(getActivity(), Login.class);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(loginIntent);

            }

        }
    };

    mDatabase = FirebaseDatabase.getInstance().getReference().child("reports");
    mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
    mDatabaseLike =FirebaseDatabase.getInstance().getReference().child("Likes");

    mDatabase.keepSynced(true);
    mDatabaseUsers.keepSynced(true);
    mDatabaseLike.keepSynced(true);

    mBlogList = (RecyclerView) view.findViewById(R.id.blog_list);
    mBlogList.setHasFixedSize(true);
    mBlogList.setLayoutManager(new LinearLayoutManager(getContext()));



    return view;

}

@Override
public void onStart() {
    super.onStart();


    FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
            Blog.class,
            R.layout.post_row,
            MyReports.BlogViewHolder.class,
            mDatabase

    ) {
        @Override
        protected void populateViewHolder(MyReports.BlogViewHolder viewHolder, Blog model, final int position) {

            final String post_key =getRef(position).getKey();

            viewHolder.setOutreach(model.getOutreach());
            viewHolder.setDate(model.getDate());
            viewHolder.setNamee(model.getNamee());
            viewHolder.setPhone(model.getPhone());
            viewHolder.setPartici(model.getPartici());
            viewHolder.setLesson(model.getLesson());

            viewHolder.setImage(getActivity().getApplicationContext(),model.getImage());
            viewHolder.setImage1(getActivity().getApplicationContext(),model.getImage1());
            viewHolder.setImage2(getActivity().getApplicationContext(),model.getImage2());
            viewHolder.setImage3(getActivity().getApplicationContext(),model.getImage3());

            viewHolder.setImage4(getActivity().getApplicationContext(),model.getImage4());

            viewHolder.setLikeBtn(post_key);

            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    //Toast.makeText(getActivity(), post_key, Toast.LENGTH_SHORT).show();

                    Intent singleBlogIntent = new Intent(getActivity(), PostSingleActivity.class);
                    singleBlogIntent.putExtra("blog_id", post_key);
                    startActivity(singleBlogIntent);
                }
            });

            viewHolder.mLikebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    mProcessLike = true;

                    if(mProcessLike){

                        mDatabaseLike.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {

                                if(dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){

                                }else {
                                    mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).setValue("RandomValue");
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                    }

                }
            });

        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);


}

public static class BlogViewHolder extends RecyclerView.ViewHolder{

    View mView;
    ImageButton mLikebtn;

    DatabaseReference mDatabaseLike;
    FirebaseAuth mAuth;


    public BlogViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
        mLikebtn = (ImageButton) mView.findViewById(R.id.like_btn);

        mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
        mAuth = FirebaseAuth.getInstance();

        mDatabaseLike.keepSynced(true);
    }

    public void setLikeBtn(final String post_key){

        mDatabaseLike.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if(dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){

                    mLikebtn.setImageResource(R.drawable.ic_new_like_blue);

                }else {

                    mLikebtn.setImageResource(R.drawable.ic_new_like_red2);

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    public void setOutreach(String title){
        TextView post_title = (TextView) mView.findViewById(R.id.post_outreach);
        post_title.setText(title);
    }

    public void setDate(String desc){
        TextView post_desc = (TextView) mView.findViewById(R.id.post_date);
        post_desc.setText(desc);
    }

    public void setNamee(String username){
        TextView post_username = (TextView) mView.findViewById(R.id.post_namee);
        post_username.setText(username);
    }

    public void setPhone(String date){
        TextView post_date = (TextView) mView.findViewById(R.id.post_phone);
        post_date.setText(date);
    }

    public void setPartici(String phone){
        TextView post_phone = (TextView) mView.findViewById(R.id.post_partici);
        post_phone.setText(phone);
    }


    public void setLesson(String date){
        TextView post_date = (TextView) mView.findViewById(R.id.post_lesson);
        post_date.setText(date);
    }


    public void setImage(Context ctx, String image){
        ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
        Picasso.with(ctx).load(image).into(post_image);
    }

    public void setImage1(Context ctx, String image1){
        ImageView post_image1 = (ImageView) mView.findViewById(R.id.post_image1);
        Picasso.with(ctx).load(image1).into(post_image1);
    }

    public void setImage2(Context ctx, String image2){
        ImageView post_image2 = (ImageView) mView.findViewById(R.id.post_image2);
        Picasso.with(ctx).load(image2).into(post_image2);
    }

    public void setImage3(Context ctx, String image3){
        ImageView post_image3 = (ImageView) mView.findViewById(R.id.post_image3);
        Picasso.with(ctx).load(image3).into(post_image3);
    }

    public void setImage4(Context ctx, String image4){
        ImageView post_image4 = (ImageView) mView.findViewById(R.id.post_image4);
        Picasso.with(ctx).load(image4).into(post_image4);
    }
}


public void onResume(){
    super.onResume();

    // Set title bar
    ((MainActivity) getActivity())
            .setActionBarTitle("Reports");

}

}