I want to read back the data in Firebase which is medical = "Diabetes" which key in by the user.If this user has the medical history of diabetes will display something not allow the user to buy. Anyone can teach me how to write this condition in the android studio?
Firebase data structure
public class Pain_and_Fever extends AppCompatActivity implements View.OnClickListener{
private Button btnSubmit, btnCancel;
private String userID;
Query query;
//add Firebase Database stuff
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
#Override
protected void onCreate(#Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pain_and__fever);
btnSubmit = (Button) findViewById(R.id.bttnsubmit);
btnCancel = (Button) findViewById(R.id.bttncancel);
//declare the database reference object. This is what we use to access the database.
//NOTE: Unless you are signed in, this will not be useable.
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
final FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
myRef = mFirebaseDatabase.getReference();
btnSubmit.setOnClickListener(this);
btnCancel.setOnClickListener(this);
query = myRef.orderByChild("medical").equalTo("Diabetes");
}
private void submit(){
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot issue : dataSnapshot.getChildren()){
UserInformation uInfo = issue.getValue(UserInformation.class);
if (uInfo.getMedical().equals("Diabetes")){
startActivity(new Intent(getApplicationContext(),Medicine.class));
}else{
myRef.child("Medicines").child("Pain and Fever").child(userID).setValue("Acetaminophen");
startActivity(new Intent(getApplicationContext(),Medicine.class));
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onClick(View view) {
if (view == btnSubmit){
submit();
}
if (view == btnCancel){
startActivity(new Intent(this,Medicine.class));
}
}
}
Try this way, this works for me
Query chatRoomsQuery = mFirebaseDatabase.orderByChild("medical").equalTo("your value");
chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
search_list=new ArrayList<SearchModel>();
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do something with the individual "issues"
UserRegisterModel mModel = issue.getValue(UserRegisterModel.class);
if(mModel.getArea().equals(sel_area))
hidepDialog();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Related
I want to retrieve data of specific node (clicksRemain) of current online user from firebase into a textView but the data is not showing. Below is my code.
PracticeActivity
public class PracticeActivity extends AppCompatActivity {
TextView link1;
DatabaseReference dRef;
FirebaseDatabase firebaseDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
link1 = findViewById(R.id.link1);
firebaseDatabase = FirebaseDatabase.getInstance();
dRef = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
dRef.child("clicksRemain").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String remainingClicks = snapshot.getValue(String.class);
link1.setText(remainingClicks);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}else{
link1.setText("10");
}
}
}
I think the code is correct to check if a user is logged in or not as if a user is logout then the app shows the value 10 as written in this code.
Snapshot of database
Thanks in advance
I have done these changes and now successfully retrieved the data.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uID = user.getUid();
dRef = FirebaseDatabase.getInstance().getReference("Users");
dRef.child(uID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String remainingClicks = String.valueOf(snapshot.child("clicksRemain").getValue(Integer.class));
link1.setText(remainingClicks);
}
Hello I am new in firebase and I need help with display subchildren of firebase which contain the same userID as signed user
here is users and edu1 full show of user
I need display just subchildren of signed user
and here is my code
private FirebaseAuth fAuth;
private DatabaseReference fNotesDatabase;
ArrayAdapter adapter;
ListView listView;
ArrayList<String> arraylist = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_education);
fAuth = FirebaseAuth.getInstance();
listView = findViewById(R.id.listview);
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education");
fNotesDatabase.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String values = datas.getKey();
arraylist.add(values);
adapter = new ArrayAdapter(Education.this, android.R.layout.simple_list_item_single_choice, arraylist);
listView.setAdapter(adapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Can you try this code and see if it is working with you :
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// get the current user-id to read data easly
String currentUserId = mAuth.getCurrentUser().getUid();
DatabaseReference userRef = FirebaseDatabase.getInstance()
.getReference().child("notes").child("education");
// get data to them
userRef.child(currentUserId).addValueEventListener(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot datas: dataSnapshot.getChildren()){
String values = datas.getKey();
arraylist.add(values);
adapter = new ArrayAdapter(Education.this,
android.R.layout.simple_list_item_single_choice,
arraylist);
listView.setAdapter(adapter);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You seem to have a data structure like /Education/Notes/$uid. So to just read the notes of the currently signed in user, do:
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
fNotesDatabase.child(uid).addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
...
Try the following:
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String userId = currentFirebaseUser.getUid();
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education");
fNotesDatabase.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String values = datas.getKey();
db = FirebaseDatabase.getInstance().getReference().child(values).orderByKey().equalTo(userId);
db.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get data
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here you add reference to education then retrieve the direct children under education then using orderByKey() you can check if there is a key equal to the current logged in user and retrieve the data.
I want to retrieve data from firebase with authentication user. I use ListView to display the data. But it do not show up after running.
public class InfoDisplayActivity extends AppCompatActivity {
private ListView listViewINFO;
DatabaseReference databaseReference;
FirebaseAuth firebaseAuth;
List<UserInformation> infoList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_display);
listViewINFO = findViewById(R.id.listViewINFO);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() == null){
finish();
startActivity(new Intent(this,RegisterActivity.class));
}
//FirebaseUser user = firebaseAuth.getCurrentUser();
infoList = new ArrayList<>();
}
#Override
protected void onStart() {
super.onStart();
databaseReference = FirebaseDatabase.getInstance().getReference("User Information");
FirebaseUser user = firebaseAuth.getCurrentUser();
String UserID = user.getUid();
databaseReference.child("User Information").child(UserID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot INFOSnapshot : dataSnapshot.getChildren()){
UserInformation userInfo = INFOSnapshot.getValue(UserInformation.class);
infoList.add(userInfo);
}
UserInfoAdapter userInfoAdapter = new UserInfoAdapter(InfoDisplayActivity.this, infoList);
listViewINFO.setAdapter(userInfoAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
I follow this tutorial on YouTube.
The problem in your code is that you are using the User Information reference twice. Once when declaring the databaseReference object:
databaseReference = FirebaseDatabase.getInstance().getReference("User Information");
// ^
And second when you attach the listener:
databaseReference.child("User Information").child(UserID).addListenerForSingleValueEvent(/* ... */);
// ^
To solve this, simply remove .child("User Information") from the above line of code:
databaseReference.child(UserID).addListenerForSingleValueEvent(/* ... */);
I'm trying to get the unique child name but its always returning the same id or it's returning what I enter in quotes for child.
uid hold "users" instead of traveling into users
Im trying to get the child name under users which is the long key/string:
public class displayUserBooks extends AppCompatActivity {
ListView listViewBooks;
List<existingBooks> existingBookses;
private FirebaseAuth mAuth;
FirebaseDatabase databaseViewbooks = FirebaseDatabase.getInstance();
DatabaseReference myRef = databaseViewbooks.getReference("Books");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_user_books);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
listViewBooks= (ListView) findViewById(R.id.listViewBooks);
existingBookses = new ArrayList<>();
}
#Override
protected void onStart() {
super.onStart();
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
checkBooks(dataSnapshot);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void checkBooks(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
String uid=ds.child("users").getKey();
existingBooks eBooks = new existingBooks();
eBooks = ds.getValue(existingBooks.class);
// if(user.getUid().equals(uid))
existingBookses.add(eBooks);
}
Booklist adapter = new Booklist(displayUserBooks.this,existingBookses);
listViewBooks.setAdapter(adapter);
}}
enter image description herethe root cause most likely is in the construction of the reference to the child node. It may be defined "too deep". Please refer to the snapshot of hierarchical picture of the DB tree, and portion of working code below. Inside the listener, it's traveling into the data of every user (defined by user ID):
FirebaseDatabase frDb = FirebaseDatabase.getInstance();
DatabaseReference frDbRef = frDb.getReference("apps/gastracking").child("users").child(mAuth.getCurrentUser().getUid());
ChildEventListener chEvLst = frDbRef.addChildEventListener(new ChildEventListener() {
ArrayList<RawReport> listOfRawreports = new ArrayList<RawReport>();
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getChildrenCount() > 0) {
for (DataSnapshot ds1 : dataSnapshot.getChildren())
//<...... Put your code here.....>
}
}
}
Hierarchical structure of the DB nodes: unfortunately I can't add flat snapshot - so please refer to the link at the beginning of my answer.
I have data in database:
database in firebase
I want read all child (Belgia, Czechy, Polska...) and display it in Text Field, but AFTER click the button (I don't change data in database).
The button have assigned below function:
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
txt_opis_panstwa.setText(value);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Befrore i write in code:
FirebaseDatabase database;
DatabaseReference myRef;
database = FirebaseDatabase.getInstance();
myRef = database.getReference("kraj");
Unfortunately, when I press the button nothing happens.
I will be grateful for the help
Regards, Marcin
You have to iterate through your children
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for (DataSnapshot country : dataSnapshot.getChildren()) {
txt_opis_panstwa.append(country.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference krajRef = rootRef.child("flagiistolice").child("kraj");
public void f_btn_zaczytaj_dane(View view) {
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String country = ds.getkey();
txt_opis_panstwa.append(country);
Log.d("TAG", country);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
krajRef.addListenerForSingleValueEvent(eventListener);
}