从数据库成功检索数据后,医疗 ID 片段未更新

Posted

技术标签:

【中文标题】从数据库成功检索数据后,医疗 ID 片段未更新【英文标题】:Medical ID Fragment not updating after successfully retrieving data from database 【发布时间】: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_
    android:layout_
    tools:context=".RetrieveMedicalInfo"
    android:background="@android:color/white">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/medical_image"
        android:layout_
        android:layout_
        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_
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rname"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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 &amp; 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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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 实时数据库结构。 实时数据库中的一切似乎都很好,我认为显示数据库数据的实际片段存在问题。我已经相应地更新了帖子 【参考方案1】:

您不隶属于医疗 ID 片段。试试这个

MedicalIDFragment

  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 
    TextView a, b, c, d, e, f, g, h;
    private CircleImageView userProfileImage;
    DatabaseReference RootKey;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_medicalid, container, false);

         userProfileImage = findViewById(R.id.medical_image);

        a = view.findViewById(R.id.rname);
        b = view.findViewById(R.id.rage);
        c = view.findViewById(R.id.rheight);
        d = view.findViewById(R.id.rweight);
        e = view.findViewById(R.id.rbloodtype);
        f = view.findViewById(R.id.rcondition);
        g = view.findViewById(R.id.rreaction);




        RootKey = FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("User Medical Profile");
        RootKey .addValueEventListener(new ValueEventListener()
        
         @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            
              if (dataSnapshot.exists())
                  

              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();
              String retrieveProfileImage = dataSnapshot.child("image").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);
                 Picasso.get().load(retrieveProfileImage).into(userProfileImage);

                                

                            

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError)
                            

                            
                        );

    
return view;
    
    

MedicalIDFragment 片段

<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_
    android:layout_
    tools:context=".MedicalIDFragment"
    android:background="@android:color/white">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/medical_image"
        android:layout_
        android:layout_
        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_
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rname"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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 &amp; 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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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片段,则您不隶属于医疗ID片段。试试上面的 现在它只是崩溃了,我将在调试器中运行它,看看它给出了什么错误,谢谢你到目前为止的帮助。 图片路径/users/userid/。但是你的路径 /users/userid/user/medical profile 所以我发现了错误,调试器这样说:java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View de.hdodenhof.circleimageview.CircleImageView.findViewById(int)' on a null object reference 这一行:userProfileImage = findViewById(R.id.medical_image); 导致错误说 findviewByID 应该改为:userProfileImage = userProfileImage.findViewById(R.id.medical_image); 然后错误发生跨度>

以上是关于从数据库成功检索数据后,医疗 ID 片段未更新的主要内容,如果未能解决你的问题,请参考以下文章

检索数据未出现在 ListView 的片段中

从片段向数据库中插入值时ListView不更新

我的RESTful API没有更新数据库,但是ajax返回成功

登录成功后向片段或页面传递数据

在角度6中修改数组数据后,视图未更新

在magento中成功支付paypal后检索数据