How to Get Specific Pushedid in Firebase

How to get specific pushedID in Firebase?

There are two ways in which you can achieve this. So if you want to access a specific comment, you must know something that unique identifies that pcommentll. The first solution would be to store that random id in a variable in the exact moment when you are pushing a new comment to the database using the push() method. To get that id, you can use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String commentId = rootRef
.child(UserId)
.child(PhotoId)
.child("comments")
.push()
.getKey();

You can also save that id inside the comment object if you need to use it later. So this is how it looks like in code:

Map<String, Object> map = new HashMap<>();
map.put("noOfLikes", 0);
map.put("commentId", commentId);
rootRef
.child(UserId)
.child(PhotoId)
.child("comments")
.child(commentId)
.updateChildren(map);

Once you have this key, you can use it in your reference to update the number of likes. This type of operation is usually done using Firebase transaction and for that I recommend see my answer from this post in which I have explained how to update a score property but same principle aplly in the case of likes.

The second approach would be to create an entire new top level collection like this:

Firebase-rot
|
--- comments
|
--- commentId
|
--- noOfLikes: 1

Using this solution it will be more easy for you to query the database becuase to get the number of likes you'll need only this simple reference:

DatabaseReference noOfLikesRef = rootRef
.child("comments")
.child(commentId)
.child("noOfLikes");
noOfLikesRef.addListenerForSingleValueEvent(/* ... */);

Get the pushed ID for specific value in firebase android

UPDATE 1:
it can obtain also by one line

String key = mDatabase.child("posts").push().getKey();

//**************************************************************//

after searching and trying a lot of things i came to 2 ways to do that
.
1. first one to get the key when i upload the post to the server via this function

 public void uploadPostToFirebase(Post post) {
DatabaseReference mFirebase = mFirebaseObject
.getReference(Constants.ACTIVE_POSTS_KEY)
.child(post.type);
mFirebase.push().setValue(post);
Log.d("Post Key" , mFirebase.getKey());
}
  1. i used it in my code to get the key after i have already pushed it to node for it in my database

    public void getUserKey(String email) {

    Query queryRef = databaseRef.child(Constants.USERS_KEY)
    .orderByChild(Constants.USERS_EMAIL)
    .equalTo(email);

    queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    //TODO auto generated
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    //TODO auto generated;
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    //TODO auto generated;
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    //TODO auto generated
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    //TODO auto generated
    }
    });
    }

Get the pushed key_id for specific value in firebase android

Try adding one more variable for the reference of your push:

DatabaseReference pushRef = mDatabaseRef.child("users").push();
String key_ID = pushRef.getKey();
pushRef.setValue(My_Value_Object);

Save pushed id in Firebase Model

Both solutions guarantee data consistency, but solution A is better than solution B when it comes to database size.

By using solution B, your database structure would look like this:

"posts":{
"nTY5U5NJJbPTJaPksPRNqau15H53" : {
"message" : "Hello World",
"sender" : "Rosário Pereira Fernandes",
"key" : "nTY5U5NJJbPTJaPksPRNqau15H53"
}
}

This should use around 180 bytes of disk space. Notice that this key is repeated twice on your node. Why not remove it to save space?

Using solution B, you'd have a smaller database:

"posts":{
"nTY5U5NJJbPTJaPksPRNqau15H53" : {
"message" : "Hello World",
"sender" : "Rosário Pereira Fernandes"
}
}

This would use around 135 bytes. That's 45 bytes less than solution B. Now imagine if you had 1000 posts on your database. You'd be using 45000 bytes more on solution B. This is enough space to store around 300 more posts, but it is being taken by the extra key attribute.

Don't forget that the Firebase Database has some price limitations for GB stored and GB downloaded. By using solution B you would reach this limit faster than by using solution A.

How to retreive all of the data of a specific pushed ID Child from Firebase or, From a infoWindow

Edit your code to be like below.but you need to set data into your post object. you will attach your post data into your marker using mMarker.setTag(post); for more information have a look at this link.

   FirebaseUtils.getPostRef().orderByKey().addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
latitud = dataSnapshot.getValue(Post.class).getLatitud();
longitud = dataSnapshot.getValue(Post.class).getLongitud();

titulo = dataSnapshot.getValue(Post.class).getTitulo();
costo = dataSnapshot.getValue(Post.class).getCosto();
mes = dataSnapshot.getValue(Post.class).getMes();
numeroDia = dataSnapshot.getValue(Post.class).getDia();
organizadoPor = dataSnapshot.getValue(Post.class).getOrganizadoPor();
descripcion = dataSnapshot.getValue(Post.class).getDescripicion();
hora = dataSnapshot.getValue(Post.class).getHora();
minutos = dataSnapshot.getValue(Post.class).getMinutos();

Post post = new Post();
// add all your data to post object e.g post.setLatitud(latitud);

BitmapDescriptor bm = BitmapDescriptorFactory.fromResource(R.mipmap.m5);
LatLng latLng1 = new LatLng(latitud, longitud);
mMarker = new MarkerOptions().position(latLng1).title(titulo).snippet(costo).icon(bm);
mp.add(mMap.addMarker(mMarker));

// Associate your post data with the marker
mMarker.setTag(post);

}
});

use this method to detect marker clicks

private void onMarkerClicked() {
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(HomeActivity.this, OtherActivity.class);
Bundle bundle = new Bundle();
// get the data from clicked marker and attach it with the intent
bundle.putSerializable("post", (Post) marker.getTag());
intent.putExtras(bundle);
startActivity(intent);
return true;
}
});
}

in your OtherActivity use this to get the data .

  Post mPost = (Post) getIntent().getSerializableExtra("post");

your Post model could be like this.

public class Post implements Serializable{

private String latitud,longitud,costo,titulo,mes,numeroDia,organizadoPor,descripcion,hora,minutos ;

public String getLatitud() {
return latitud;
}

public void setLatitud(String latitud) {
this.latitud = latitud;
}

public String getLongitud() {
return longitud;
}

public void setLongitud(String longitud) {
this.longitud = longitud;
}

public String getCosto() {
return costo;
}

public void setCosto(String costo) {
this.costo = costo;
}

public String getTitulo() {
return titulo;
}

public void setTitulo(String titulo) {
this.titulo = titulo;
}

public String getMes() {
return mes;
}

public void setMes(String mes) {
this.mes = mes;
}

public String getNumeroDia() {
return numeroDia;
}

public void setNumeroDia(String numeroDia) {
this.numeroDia = numeroDia;
}

public String getOrganizadoPor() {
return organizadoPor;
}

public void setOrganizadoPor(String organizadoPor) {
this.organizadoPor = organizadoPor;
}

public String getDescripcion() {
return descripcion;
}

public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}

public String getHora() {
return hora;
}

public void setHora(String hora) {
this.hora = hora;
}

public String getMinutos() {
return minutos;
}

public void setMinutos(String minutos) {
this.minutos = minutos;
}
}

How to get a key of an object that was pushed just now in Firebase?

To get the key, please use the code below:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference keyRef = rootRef.push();
String key = keyRef.getKey();
keyRef.setValue(user);

or

Firebase fire = new Firebase(FirebaseConfig.URL) ;
String pushKey = fire.push().getKey();
fire.child(pushKey).setValue(user);

Hope it helps.

How to get the push id of a specific value in android using firebase

Try this:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(TOP_NODE_NAME).orderByChild("topic_name").equalTo("algebre");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String key = child.getKey();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

Now key has the Key of the node where topic_name=algebra

How to get pushed id from recyclerview in datasnapshot?

You are using dataSnapshot instead of dataSnapshot1. Use must use dataSnapshot1 to get the key of the Therapist objects

for(DataSnapshot dataSnapshot1: dataSnapshot.child("thera").getChildren()){
Therapist thera= dataSnapshot1.getValue(Therapist.class);
tkey.add(dataSnapshot1.getKey()); // <-- Here you need to use dataSnapshot1
t.add(thera);
}


Related Topics



Leave a reply



Submit