Reputation: 36
I want to retrieve eventName and eventSport values found in each userID node.
I am still new to Android Studio.
My firebase database is organized by:
fir-login-31***
event
userID(retrieved from firebase authentication)
Unique EventID(randomly generated by me)
eventID: "Unique EventID
eventName: "Test1
eventSport: "TestSport12
Because every time a different user uploads data into the database, the data is stored within their firebase UserID Node from Firebase Authentication. Is there a way for me to still retrieve data from each user-generated node while still letting users store their data by their own UserID? I use their userID to setup read-write rules in FirabaseDatabase.
Host_Activity.java:
package com.example.songkwongwee.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class HostActivity extends AppCompatActivity {
EditText editTextName;
Button buttonAdd;
Spinner spinnerSports;
DatabaseReference databaseEvent;
FirebaseAuth mAuth;
ListView listViewEvents;
List<Event> eventList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host);
databaseEvent = FirebaseDatabase.getInstance().getReference("event");
mAuth = FirebaseAuth.getInstance();
editTextName = findViewById(R.id.editTextName);
buttonAdd = findViewById(R.id.buttonHostGame);
spinnerSports = findViewById(R.id.spinnerSports);
listViewEvents = findViewById(R.id.listViewEvents);
eventList = new ArrayList<>();
buttonAdd.setOnClickListener(new View.OnClickListener() { //click button method for adding event
@Override
public void onClick(View v) {
addEvent();
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseEvent.addValueEventListener(new ValueEventListener() { //executed every time we change anything in database
@Override
public void onDataChange(DataSnapshot dataSnapshot) { //contains all data in snapshot data object
eventList.clear(); //clear artist list
for(DataSnapshot eventSnapshot: dataSnapshot.getChildren()){
Event event = eventSnapshot.getValue(Event.class);
eventList.add(event); //every time new data entered, this method executes,fetch all artist from database
}
EventList adapter = new EventList(HostActivity.this, eventList);
listViewEvents.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void addEvent(){ //method for user to create event
String name = editTextName.getText().toString().trim();
String sport = spinnerSports.getSelectedItem().toString();
if(!TextUtils.isEmpty(name)){ //if name is not empty
String uid = mAuth.getUid(); //call uid method to get user id
String id = databaseEvent.push().getKey(); //call id method to get random string key
Event event = new Event(id, name, sport); //call event method to
databaseEvent.child(uid).child(id).setValue(event); //method to store node in organise form (Event>UserID>Event.UKey>Details)
Toast.makeText(this,"Event added",Toast.LENGTH_LONG).show(); //Show UI
}
else{ //if name is empty
Toast.makeText(this,"You should enter a name", Toast.LENGTH_LONG).show(); //Show error UI
}
}
}
Event.java:
public class Event {
String eventId;
String eventName;
String eventSport;
public Event(String eventId, String eventName, String eventSport){
this.eventId = eventId;
this.eventName = eventName;
this.eventSport = eventSport;
}
public String getEventId() {
return eventId;
}
public String getEventName() {
return eventName;
}
public String getEventSport() {
return eventSport;
}
Upvotes: 2
Views: 1855
Reputation: 80914
To retrieve eventname and eventsport, try the following:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("event").child(user.getUid());;
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String eventname=datas.child("eventName").getValue().toString();
String eventsport=datas.child("eventSport").getValue().toString();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
You have this database:
event
userID(retrieved from firebase authentication)
Unique EventID(randomly generated by me)
eventID: "Unique EventID
eventName: "Test1
eventSport: "TestSport12
each userid is unique for that user, and since you want the user to retrieve his data then you can use user.getUid()
which will retrieve the userid and then iterate inside of it to get the eventname and eventsport.
Upvotes: 1