更新:我已设法从我的数据库中检索特定的狗数据(请参阅 .child("moo")
在下面我将push()元素从 SignUp
类,以便将狗的名称用作id,而不是唯一的id。但是,如果用户有多只狗,那么最好的方法是什么?我如何遍历特定的狗?我如何才能显示特定的狗信息?
我正在尝试从firebase数据库中检索数据,并在 TextView
,我已经在项目的其他地方完成了这项工作,没有任何问题,但由于某些原因,无法从构造函数类检索get/set方法。我正在寻找一些帮助,找出原因,因为我已经做了几个小时了,但找不到解决方案。。
基本上,我想从每个独特的狗的用户下检索数据,并在相应的 TextView
我的dogprofile类中的元素。这段代码不起作用,它抛出了一个nullobjectref错误,但我真的不知道如何修复它,我想我需要弄清楚如何访问当前的dog元素?如果用户有不止一只狗,那么这可能是一个问题。。
我的firebase Hiarachy:
狗狗建造师:
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; }
}
狗资料类别:
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>
1条答案
按热度按时间ndasle7k1#
因为它在列表中,所以我认为您想要检索狗的列表。你需要使用
for loop
.