This is the structure of the database
I need to fetch all the values of "QNGGvYL7dgde9Oypi9a4IFdAys03"
I have tried this code but it's not working
String uid = FirebaseAuth.getInstance().getUid();
DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference();
Query query = databaseRef.child("bookmarks").equalTo(FirebaseAuth.getInstance().getUid());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
vehicleList.add(postSnapshot.getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
If you know the key you want to get, you don't need a query:
DatabaseReference ref = databaseRef.child("bookmarks").child(FirebaseAuth.getInstance().getUid());
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
vehicleList.add(postSnapshot.getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errorrs
}
});
Related
This is the code which is for deleting a perticular post...
Here what i am doing is that when user click the delete button then the post should be deleted but insteed of that nothing is being deleting.Also when i put log to check whether i am getting the unique id generated by firebase database then it shows like this
'2020-04-16 10:00:41.328 29431-29431/com.example.zone D/sanyam: MBlog'
My firebase database Looks Like this(I want to delete the that unique id)
MBlog
-M4iN100Ic5TiSjRRTtX
-M4iNkUGOCd7aKSqg09q
-M4mco3s-nEo0ohXYwIm
-M4xzHyoJjE0QY-5cKeZ
```
//delete button
``` holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPostDatabase = FirebaseDatabase.getInstance().getReference().child("MBlog"); //child name must be same otherwise the
//DatabaseReference newPost = mPostDatabase.getRef();
mPostDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String uid = mPostDatabase.getKey();
Log.d("sanyam",uid);
mPostDatabase.child(uid).removeValue();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}```
The dataSnapshot is still a list. To access the item, you need to loop over the result:
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String uid = snapshot.getKey();
Log.d("sanyam",uid);
mPostDatabase.child(uid).removeValue();
}
}
But if you run this query all items will remove.
If you want to remove one item you can do it like this:
mPostDatabase = FirebaseDatabase.getInstance().getReference().child("MBlog").child(uid);
mPostDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String uid = dataSnapshot.getKey();
Log.d("sanyam",uid);
mPostDatabase.removeValue();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
If you want to remove all that value at once
mPostDatabase = FirebaseDatabase.getInstance().getReference().child("MBlog");
mPostDatabase.removeValue();
but if you want to delete particular node/ref
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPostDatabase = FirebaseDatabase.getInstance().getReference().child("MBlog"); //child name must be same otherwise the
//DatabaseReference newPost = mPostDatabase.getRef();
mPostDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String uid = dataSnapshot.getKey();
Log.d("sanyam",uid);
if(uid.equal("M4mco3s-nEo0ohXYwIm")){//Here you can check UID
removeDataRef(dataSnapshot.getRef())
}
}
//mPostDatabase.child(uid).removeValue();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
And If you want to get callback of remove complete here is the listener
public void removeDataRef(DatabaseReference ref){
ref.removeValue(new DatabaseReference.CompletionListener() {
#Override
public void onComplete(#Nullable DatabaseError databaseError, #NonNull DatabaseReference databaseReference) {
//Here you can check the operation of remove completed or not
}
});
}
i'm a beginner in android application, could anyone help me and show me how could i retrieve the generated id value node of the current user Id ? please find below my code. Thanks
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
final String userKey = user.getUid();
mDb.child("students").child(currentuser).getRef().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String reference = dataSnapshot.getRef().toString();
String userID = dataSnapshot.child("level").getValue(String.class);
textViewStudentName.setText(reference);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try below to get level:
DatabaseReference fireReference = FirebaseDatabase.getInstance().getReference("students");
fireReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
if(childSnapshot.hasChild(userKey)) {
String userID = childSnapshot.child(userKey).child("level").getValue(String.class);
break;
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I have been making a project using firebase for a delivery system app.
Problem: In this image, I need to get these two keys(or parent I think) and store them in a string or if possible I just want to know if I can check if the Id in user has a duplicate. How to check if these key has duplicate or exist and store it on a string if it does exist.
Updated code,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manageordergrocery);
mRecyclerView2= findViewById(R.id.recyclerview1);
mRecyclerView2.setHasFixedSize(true);
mRecyclerView2.setLayoutManager(new LinearLayoutManager(this));
mModel2 = new ArrayList<>();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
mAdapter2=new ViewHolder1(Manageordergrocery.this, mModel2);
mRecyclerView2.setAdapter(mAdapter2);
mDatabaseReference2= FirebaseDatabase.getInstance().getReference("Users");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
String myParentNode = postSnapshot.getKey();
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference();
mDatabaseReference2.orderByKey().equalTo(myParentNode).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String saveNode = dataSnapshot.getKey();
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference(saveNode).child("Ordered");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
Model1 model2 = postSnapshot.getValue(Model1.class);
mModel2.add(model2);
}
mAdapter2.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Manageordergrocery.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Can someone help me, please? I'm still new in firebase and don't know how to do it.
To obtain the two keys you can do the following:
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference("User");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
String myParentNode = postSnapshot.getKey();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.orderByKey().equalTo(myParentNode).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot posts :dataSnapshot.getChildren())
{
String key = posts.getKey();
if(key.equals(myParentNode))
Toast.makeText(Manageordergrocery.this,"equals", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Manageordergrocery.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
Here first you add a reference to node User then you loop inside that node and retrieve the key using getKey(). After that you attach another listener to the root location and using orderByKey().equalTo(myParentNode) you can check if you have two keys with the same value and do the required.
Hello how can i delete the node in fire_base database using java ?
i have tried this method but it didn't work.
This image show the node struct
the node struct
private void deleteTask (final String userName){
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("All_USER");
reference.orderByChild("userName").equalTo(userName).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds :dataSnapshot.getChildren()){
String userName = (String) ds.child("userName").getValue();
reference.child(userName).setValue(null);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
this code is work for me
private void deleteTask (final String userName){
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("All_USER");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds :dataSnapshot.getChildren()){
if(ds.child("NewUser").child("userName").getValue(String.class).equals(userName)){
ds.child("NewUser").child("userName").getRef().removeValue();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Try this code:
private void deleteTask (final String userName){
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("All_USER");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds :dataSnapshot.getChildren()){
ds.getRef().orderByChild("userName").equalTo(userName).removeValue();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
You can just change your code like this :
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (snapshot in dataSnapshot.children) {
snapshot.reference.removeValue()
}
}
}
I have this database:
In Activity A, I click on a class(listitem) and then pass the id of that class to this activity like this:-
so = getIntent().getStringExtra("send_id");
So right now I have the ClassId in this activity inside so. Then I run this query:
myref= FirebaseDatabase.getInstance().getReference().child("ClassStudent");
Query fk=myref.orderByChild("ClassId").equalTo(so);
fk.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
student=dataSnapshot.child("Studentid").getValue().toString();
//a breakpoint here gives student=oyvEbB0128Y90ADzXnL0UwCUy0Z2
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}
So in the query I want all the students id that have that classid orderByChild("ClassId").equalTo(so)
Then I create a FirebaseRecycleradapter
FirebaseRecyclerAdapter<Info,ViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Info,ViewHolder>(Info.class, R.layout.row, ViewHolder.class, fk) {
#Override
protected void populateViewHolder(final ViewHolder holder, Info model, int position) {
holder.title.setText(student); //a breakpoint here gives student = 7AnhQ7pRwhXDRqytjSUkoJrOB253
}
}
Now the data is appearing(on phone screen) like this:
Result
7AnhQ7pRwhXDRqytjSUkoJrOB253
7AnhQ7pRwhXDRqytjSUkoJrOB253
The above student id is appearing twice, instead it should be:
It should be
7AnhQ7pRwhXDRqytjSUkoJrOB253
oyvEbB0128Y90ADzXnL0UwCUy0Z2
Since both those students are in that class.
I have also overridden, getItemId and getItemViewType
To solve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child("ClassStudent");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String Studentid = ds.child("Studentid").getValue(String.class);
Log.d("TAG", Studentid);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);
The output will be:
7AnhQ7pRwhXDRqytjSUkoJrOB253
oyvEbB0128Y90ADzXnL0UwCUy0Z2
Edit: To get the name of the student of the coreponding stundet if you need to query your database twice. So, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child("ClassStudent");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String Studentid = ds.child("Studentid").getValue(String.class);
Log.d("TAG", Studentid);
DatabaseReference studentIdRef = rootRef.child("Student").child(Studentid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = ds.child("name").getValue(String.class);
Log.d("TAG", name);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
studentIdRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);