如何在两个不同的活动中从Firebase实时数据库检索数据

时间:2019-02-09 13:42:45

标签: java android firebase-realtime-database

我正在设置一个可以在两个不同活动中显示个人数据的android应用。数据存储在我的Firebase数据库中,有人可以帮助我吗?

我已经尝试在两个Java类中都使用addValueEventListener。负责在“用户个人资料”活动中显示用户信息的用户类已成功从我的Firebase检索了数据,但另一个仍无法从我的Firebase检索数据。 这是来自Java类的代码,无法从我的Firebase检索数据。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_land_profile);

    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();

    mDBase = FirebaseDatabase.getInstance();


    nama_lahan = (TextView) findViewById(R.id.nama_lahan);
    lebar_lahan = (TextView) findViewById(R.id.lebar_lahan);
    panjang_lahan = (TextView) findViewById(R.id.panjang_lahan);
    luas_lahan = (TextView) findViewById(R.id.luas_lahan);
    jenis_lahan = (TextView) findViewById(R.id.jenis_lahan);
    jenis_pupuk = (TextView) findViewById(R.id.jenis_pupuk);
    public void fetchLandData (String userUID){
    mAkun = mDBase.getReference("Akun").child(userUID);
    mAkun.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot landData : dataSnapshot.getChildren()){
                Lahan lahan = landData.getValue(Lahan.class);

                if(lahan != null){
                    namaLahan = lahan.getNamaLahan();
                    jns_lahan = lahan.getJenisLahan();
                    jns_pupuk = lahan.getJenisPupuk();
                    lebarLahan = lahan.getLebarLahan();
                    panjangLahan = lahan.getPanjangLahan();

                    luasLahan = panjangLahan*lebarLahan;

                    nama_lahan.setText(namaLahan);
                    jenis_pupuk.setText(jns_pupuk);
                    jenis_lahan.setText(jns_lahan);
                    lebar_lahan.setText(String.valueOf(lebarLahan));
                    panjang_lahan.setText(String.valueOf(panjangLahan));
                    luas_lahan.setText(String.valueOf(luasLahan));
                }else{
                    Toast.makeText(LandProfileActivity.this, "Gak iso pisan ``bos", Toast.LENGTH_SHORT).show();
                }
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

虽然另一个结构几乎相同,但变量名不同,但是它可以给我我想要的结果。

That is the screenshot from my application that failed to retrieve data from my firebaseThis is what my Firebase DB looks like

这些是我从logcat复制的一些错误:

2019-02-09 23:32:11.070 1645-3271/? E/ActivityTrigger: activityStartTrigger: not whiteListedcom.example.johno.test/com.example.johno.test.LandProfileActivity/1 2019-02-09 23:32:11.072 1645-3271/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.johno.test/com.example.johno.test.LandProfileActivity/1 2019-02-09 23:32:11.079 1645-3065/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.johno.test/com.example.johno.test.LandProfileActivity/1

1 个答案:

答案 0 :(得分:0)

您可以将Firebase ValueEvent侦听器放在Application类中:

public class MyApplication extends Application {

   //mAkun field declaration;
   private ValueEventListener value_event_listener;


    @Override
    public void onCreate() {
        mAkun = mDBase.getReference("Akun").child(userUID);
        mAkun.addValueEventListener(new ValueEventListener() {/*..*/}
    }
    @Override
    public void onTerminate() {
        super.onTerminate();
        mAkun.removeEventListener(value_event_listener);
    }
}