0

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..

My Firebase hiararchy: enter image description here

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>
Caitlin
  • 531
  • 5
  • 19

1 Answers1

1

Since it is inside the list, I think you want to retrieve list of dogs. You need to use for loop.

    breedRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String dogBreed;
            List<Dog> dogList;
             for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                final Dog dog = snapshot.getValue(Dog.class);
                dogList.add(dog);
                dogBreed += dog.getBreed();
               
            }
            breed.setText(currentUser.getBreed());
            // -------------

            Log.i("TAG", dataSnapshot.getValue(String.class));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w("TAG", "onCancelled", databaseError.toException());
        }
    });
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • I would like to retrieve the specific details (breed, for example) of a particular dog under the current user. This seems straight forward for one dog, but itlf the user has more than one dog I need to get the particular dog whose details I am viewing - hope that makes sense! I am at work right now so I will play around with your answer after then :) Thanks! – Caitlin Jul 26 '21 at 07:12
  • @Caitlin the important one is you need to get its `dogUid`. – Ticherhaz FreePalestine Jul 26 '21 at 08:09
  • Yes exactly. I am not sure how to get to that point.. – Caitlin Jul 26 '21 at 08:36
  • I do not wish to display the list of dogs, I would like to access each dog individually to retrieve details about them in order for the user (dog owner) to see and edit/update them. – Caitlin Jul 26 '21 at 18:57
  • what is `specificDog` referring to, I have an idea but can't get anything to work in its place – Caitlin Jul 26 '21 at 19:29
  • @Caitlin fixed the answer already. you can try. how do you want to retrieve details without listing the dogs? – Ticherhaz FreePalestine Jul 27 '21 at 00:25
  • On one page I would like to display the list of dogs, then each dog would have a button which would go to the dog profile page and display the details of the chosen dog. I don't understand how to achieve that – Caitlin Jul 27 '21 at 06:22
  • 1
    @Caitlin you either make a listview or using recyclerview to list out all the dogs. From there, you can make a button to view detail that specific dog – Ticherhaz FreePalestine Jul 27 '21 at 08:43
  • Okay, can I do this in a separate page or does it need to be in the same page? – Caitlin Jul 27 '21 at 12:32
  • it is up to you. @Caitlin which one is the best for you – Ticherhaz FreePalestine Jul 27 '21 at 12:33
  • I think the separate page would be best – Caitlin Jul 27 '21 at 13:18
  • 1
    @Caitlin https://stackoverflow.com/a/54655654/9346054 you can refer this to create recyclerview. if you still need help, Ticherhaz#7548 this is my discord – Ticherhaz FreePalestine Jul 27 '21 at 14:28