Recover Data Firebase

1

With the code below I retrieve data from a post, okay, with FirebaseRecyclerAdapter I retrieve the comments that are in recyclerview , ok, but the comment appears in all posts, what I want is to retrieve the comments only for the commented post.

So I send a child with the post id along with the comment, okay, I tried to use orderByChild and equalTo , but to no avail.

I have read the documentation and could not resolve it:)

public class BlogSingleActivity extends AppCompatActivity {


private String  mPost_key = null;
private DatabaseReference mDatabase;
private ImageView mBlogSingleImage;
private TextView mBlogSingleTitle;
private TextView mBlogSingleDesc;
private TextView mBlogSingleCont;
private Button mSingleRemoveBtn;
private FirebaseAuth mAuth;
private TextView mSingleBlogComent;
private Button mSingleComentBtn;
private ProgressDialog mProgress;
private Uri mImageUri = null;
private DatabaseReference mDatabaseComent;
private RecyclerView mComentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blog_single);

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
    mPost_key = getIntent().getExtras().getString("blog_id");



    mAuth = FirebaseAuth.getInstance();
    mBlogSingleDesc = (TextView) findViewById(R.id.singleBlogDesc);
    mBlogSingleTitle = (TextView) findViewById(R.id.singleBlogTitle);
    mBlogSingleImage = (ImageView) findViewById(R.id.singleBlogImage);
    mBlogSingleCont = (TextView) findViewById(R.id.singleBlogCont);
    mSingleRemoveBtn = (Button) findViewById(R.id.singleRemoveBtn);
    mSingleBlogComent = (TextView) findViewById(R.id.singleBlogComent);
    mSingleComentBtn = (Button) findViewById(R.id.singleComentBtn);
    mProgress = new ProgressDialog(this);
    mDatabaseComent = FirebaseDatabase.getInstance().getReference().child("Coment");
    mComentList = (RecyclerView) findViewById(R.id.coment_list);
    mComentList.setHasFixedSize(true);
    mComentList.setLayoutManager(new LinearLayoutManager(this));





}

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Cont, BloviewHolderr> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Cont, BloviewHolderr>(

            Cont.class,
            R.layout.coment_row,
            BloviewHolderr.class,
            mDatabaseComent


    ) {
        @Override
        protected void populateViewHolder(BloviewHolderr viewHolder, Cont model, int position) {
            final String post_key = getRef(position).getKey();
            //viewHolder.set_nome(model.getName());
            viewHolder.setComentario(model.getComentario());


        }
    };

    mComentList.setAdapter(firebaseRecyclerAdapter);

    mDatabase.child(mPost_key).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            String post_title = (String) dataSnapshot.child("title").getValue();
            String post_desc = (String) dataSnapshot.child("desc").getValue();
            String post_image = (String) dataSnapshot.child("image").getValue();
            String post_uid = (String) dataSnapshot.child("uid").getValue();
            String post_cont = (String) dataSnapshot.child("cont").getValue();


            mBlogSingleTitle.setText(post_title);
            mBlogSingleDesc.setText(post_desc);
            mBlogSingleCont.setText(post_cont);
            Picasso.with(BlogSingleActivity.this).load(post_image).into(mBlogSingleImage);


            if(mAuth.getCurrentUser().getUid().equals(post_uid)){

                //Toast.makeText(BlogSingleActivity.this, post_uid, Toast.LENGTH_LONG).show();
                //Toast.makeText(BlogSingleActivity.this, mAuth.getCurrentUser().getUid(), Toast.LENGTH_LONG).show();
                mSingleRemoveBtn.setVisibility(View.VISIBLE);

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    mSingleRemoveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabase.child(mPost_key).removeValue();
            Intent mainIntent = new Intent(BlogSingleActivity.this, MainActivity.class);
            startActivity(mainIntent);
        }
    });

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

        startComent();
    }


    private void startComent() {

        mProgress.setMessage("Postando");
        final String coment_val = mSingleBlogComent.getText().toString().trim();

        if (!TextUtils.isEmpty(coment_val)) {

            mProgress.show();


            final String user_id = mAuth.getCurrentUser().getUid();
            final String post_key = getKey();
            final DatabaseReference newComent = mDatabaseComent.push();
            mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    newComent.child("comentario").setValue(coment_val);
                    newComent.child("id_post").setValue(mPost_key);
                    //newComent.child("comentario").setValue(coment_val);
                    //mDatabase.child(post_key).child(user_id).child("coment").setValue(coment_val);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            mProgress.dismiss();
        }

    }

});}

public static class BloviewHolderr extends  RecyclerView.ViewHolder{

View mView;

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

    mView = itemView;

public void setComentario (String comentario){
    TextView post_coment = (TextView)mView.findViewById(R.id.comentComent);
    post_coment.setText(comentario);

}


}}

RESOLVED

StringcurrentUserId=mPost_key=getIntent().getExtras().getString("blog_id");                                            
 mDatabaseCurrentUser = FirebaseDatabase.getInstance().getReference().child("Coment");                                      
 mQueryCurrentUser = mDatabaseCurrentUser.orderByChild("id_post").equalTo(currentUserId);                                   



}                                                                                                                              
     @Override                                                                                                                  
 protected void onStart() {                                                                                                     
     super.onStart();                                                                                                           
     FirebaseRecyclerAdapter<Cont, BloviewHolderr> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Cont, BloviewHolderr>( 

             Cont.class,                                                                                                        
             R.layout.coment_row,                                                                                               
             BloviewHolderr.class,                                                                                              
             mQueryCurrentUser                                                                                                  


     )
    
asked by anonymous 19.01.2017 / 17:23

1 answer

2

I think in this case, you need to put the list reading event:

public void onDataChange(DataSnapshot dataSnapshot) {
     lista = new ArrayList<>();
     for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
          lista.add(postSnapshot.getValue(Lista.class));
     }
}

I did it here and it worked fine:)

    
12.07.2017 / 15:53