【发布时间】:2020-08-27 09:24:22
【问题描述】:
所以我构建了一个使用片段的健康应用程序。我有一个单独的活动可以更新用户的医疗 ID。这些数据存储在 firebase 内的实时数据库中。问题是数据即使在被检索后也不会显示在我的医疗 ID 片段中。
我需要应用程序在更新医疗 ID 后更新这个特定的片段,也许有一些片段刷新方法可以帮助解决这个问题,我使用的那些已经被弃用了。
片段膨胀的类:
package com.example.managinghealthapplicationv1;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MedicalIDFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_medicalid, container, false);
}
}
从数据库中获取片段活动数据的类(Rootkey引用正确):
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
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 com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class RetrieveMedicalInfo extends AppCompatActivity {
TextView a, b, c, d, e, f, g, h;
private CircleImageView userProfileImage;
private String currentUserID;
private FirebaseAuth mAuth;
DatabaseReference RootKey;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_medicalid);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
userProfileImage = findViewById(R.id.medical_image);
a = findViewById(R.id.rname);
b = findViewById(R.id.rage);
c = findViewById(R.id.rheight);
d = findViewById(R.id.rweight);
e = findViewById(R.id.rbloodtype);
f = findViewById(R.id.rcondition);
g = findViewById(R.id.rreaction);
h = findViewById(R.id.rmedication);
RootKey = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("User Medical Profile");
RootKey.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
String retrieveProfileImage = dataSnapshot.child("image").getValue().toString();
Picasso.get().load(retrieveProfileImage).into(userProfileImage);
String name = dataSnapshot.child("name").getValue().toString();
String age = dataSnapshot.child("age").getValue().toString();
String height = dataSnapshot.child("height").getValue().toString();
String weight = dataSnapshot.child("weight").getValue().toString();
String bloodtype = dataSnapshot.child("bloodtype").getValue().toString();
String medcondition = dataSnapshot.child("medcondition").getValue().toString();
String medreaction = dataSnapshot.child("medreaction").getValue().toString();
String medmedication = dataSnapshot.child("medmedication").getValue().toString();
a.setText(name);
b.setText(age);
c.setText(height);
d.setText(weight);
e.setText(bloodtype);
f.setText(medcondition);
g.setText(medreaction);
h.setText(medmedication);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
这里是医疗ID的布局xml文件:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RetrieveMedicalInfo"
android:background="@android:color/white">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/medical_image"
android:layout_width="148dp"
android:layout_height="140dp"
android:layout_marginStart="157dp"
android:layout_marginLeft="157dp"
android:layout_marginTop="72dp"
android:layout_marginEnd="158dp"
android:layout_marginRight="158dp"
android:src="@drawable/blank_profile"
app:civ_border_color="#ff7f7f"
app:civ_border_width="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/rname"
android:layout_width="147dp"
android:layout_height="26dp"
android:layout_marginStart="111dp"
android:layout_marginLeft="111dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="111dp"
android:layout_marginRight="111dp"
android:ems="10"
android:gravity="center"
android:hint="Name"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/medical_image" />
<TextView
android:id="@+id/rage"
android:layout_width="93dp"
android:layout_height="21dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="406dp"
android:ems="10"
android:gravity="center"
android:hint="Age"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/rheight"
android:layout_width="99dp"
android:layout_height="22dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="405dp"
android:ems="10"
android:gravity="center"
android:hint="Height (cm)"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/rweight"
app:layout_constraintStart_toEndOf="@+id/rage" />
<TextView
android:id="@+id/rweight"
android:layout_width="100dp"
android:layout_height="21dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="406dp"
android:ems="10"
android:gravity="center"
android:hint="Weight (kg)"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/rbloodtype"
android:layout_width="379dp"
android:layout_height="63dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="24dp"
android:ems="10"
android:gravity="center"
android:hint="Blood Type"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/rcondition"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/rcondition"
android:layout_width="379dp"
android:layout_height="63dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="24dp"
android:ems="10"
android:gravity="center"
android:hint="Medical Conditions"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/rreaction"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/rreaction"
android:layout_width="380dp"
android:layout_height="64dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="24dp"
android:ems="10"
android:gravity="center"
android:hint="Allergies & Reactions"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/rmedication"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/rmedication"
android:layout_width="379dp"
android:layout_height="64dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="60dp"
android:ems="10"
android:gravity="center"
android:hint="Medications"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginBottom="435dp"
android:text="Age"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="111dp"
android:layout_marginLeft="111dp"
android:layout_marginEnd="97dp"
android:layout_marginRight="97dp"
android:layout_marginBottom="435dp"
android:text="Height"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView7"
app:layout_constraintStart_toEndOf="@+id/textView5" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="46dp"
android:layout_marginRight="46dp"
android:layout_marginBottom="435dp"
android:text="Weight"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在活动中更新的医疗 ID 信息:
不显示医疗 ID 片段中的任何更改:
最后这是 firebase 数据库的结构:
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
如果有任何用处,我可以将 github 链接提供给我的项目
-
更新您的 Firebase 实时数据库结构。
-
实时数据库中的一切似乎都很好,我认为显示数据库数据的实际片段存在问题。我已经相应地更新了帖子
标签: java android android-fragments firebase-realtime-database