简体   繁体   中英

How can I retrieve data from Firebase Realtime Database in two different activity

I'm setting up an android apps that could show the personal data in two different activity. The data is stored in my firebase database, can anyone help me doing that?

I already tried to use addValueEventListener in both of my java classes. User's class that is responsible to display user information in User Profile activity is successfully retrieved the data from my firebase, but the other one still couldn't retrieve the data from my firebase. This is the code from java class that couldn't retrieve the data from my 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) {

        }
    });

While the other one is pretty much the same structure but different variable name and it could give me the result that i wanted.

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

These are some error that i copied from my 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

you can put Firebase ValueEvent listener in Application class:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM