UPDATE:
I have managed to retrieve specific dog data from my database (see .child("moo")
below. I changed the push() element from my SignUp
class so that the dogs name would be used as the ID instead of a unique ID. However if the user has more than one dog, what would be the best way to do this? How can I iterate through the specific dogs? How would I be able to display a specific dogs information?
I am trying to retrieve data from my Firebase database and display it within a TextView
, I have done this elsewhere in my project without any issues but for some reason cannot retrieve my get/set methods from my constructor class. I am looking for some help figuring out why this is as I've been at it a few hours now and can't find a solution..
Basically I would like to retrieve the data from each unique individual dog under its user, and display it within the corresponding TextView
element on my DogProfile class. This code is not working, it throws a NullObjectRef error but I really don't know how to fix this, I think I need to figure out how to access the current dog element? As if the user has more than one dog then it could be an issue..
Dog constructor:
public class Dog {
private String name, breed, age, weight, gender, neutered;
public Dog() {
}
public Dog (String name, String breed, String age, String weight, String gender, String neutered) {
this.name = name;
this.breed = breed;
this.age = age;
this.weight = weight;
this.gender = gender;
this.neutered = neutered;
}
public String getName() {
return name;
}
public String getBreed() {
return breed;
}
public String getAge() {
return age;
}
public String getWeight() { return weight; }
public String getGender() { return gender; }
public String getNeutered() { return neutered; }
}
Dog profile class:
public class DogProfile extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private FirebaseUser currentUser;
TextView breed, age, dogGender, weight, isNeutered;
String uid;
String dogId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dog_profile);
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
uid = currentUser.getUid();
breed = findViewById(R.id.breed);
age = findViewById(R.id.age);
dogGender = findViewById(R.id.gender);
weight = findViewById(R.id.weight);
isNeutered = findViewById(R.id.neutered);
DatabaseReference databaseReference = FirebaseDatabase.getInstance("").getReference("user").child(uid).child("dogs");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String dogBreed = ds.child("breed").getValue(String.class);
String dogAge = ds.child("age").getValue(String.class);
String gender = ds.child("gender").getValue(String.class);
String dogWeight = ds.child("weight").getValue(String.class);
String neutered = ds.child("neutered").getValue(String.class);
breed.setText(dogBreed);
age.setText(dogAge);
dogGender.setText(gender);
weight.setText(dogWeight);
isNeutered.setText(neutered);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG", "onCancelled", databaseError.toException());
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".DogProfile">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="264dp"
android:background="@color/olive"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="393dp"
android:layout_height="560dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">
<Spinner
android:id="@+id/age"
android:layout_width="160dp"
android:layout_height="59dp"
android:autofillHints="age"
android:background="@drawable/spinner_bg"
android:hint="Age"
android:inputType="text"
android:labelFor="@+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.085"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.353" />
<Spinner
android:id="@+id/weight"
android:layout_width="160dp"
android:layout_height="61dp"
android:autofillHints="weight"
android:background="@drawable/spinner_bg"
android:hint="Weight (kg)"
android:inputType="text"
android:labelFor="@+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.901"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.356" />
<TextView
android:id="@+id/breed"
android:layout_width="347dp"
android:layout_height="57dp"
android:autofillHints="breed"
android:background="@drawable/spinner_bg"
android:focusable="true"
android:hint="Breed"
android:inputType="text"
android:labelFor="@+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.434"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.196" />
<EditText
android:id="@+id/name"
android:layout_width="354dp"
android:layout_height="56dp"
android:autofillHints="Name"
android:background="@drawable/text_field"
android:hint="Name"
android:inputType="text"
android:labelFor="@+id/name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.032"></EditText>
<Spinner
android:id="@+id/gender"
android:layout_width="160dp"
android:layout_height="61dp"
android:autofillHints="gender"
android:background="@drawable/spinner_bg"
android:hint="Gender"
android:inputType="text"
android:labelFor="@+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.086"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.532" />
<Spinner
android:id="@+id/neutered"
android:layout_width="160dp"
android:layout_height="56dp"
android:autofillHints="neutered"
android:background="@drawable/spinner_bg"
android:hint="Neutered?"
android:inputType="text"
android:labelFor="@+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.901"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Daily Exercise Goal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.058"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.665" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.624" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>