无法使用ValueEventListener从Firebase检索数据

时间:2019-05-28 12:04:20

标签: java android firebase firebase-realtime-database

This is my database tree

我正在尝试从数据库检索数据并将其显示在活动中。数据是从数据库中检索出来的,其父级对应于先前单击的RecyclerView的位置。使用ValueEventListener检索数据时,表明变量为空。

我正在使用意图将RecyclerView的位置发送到其他活动,以便它能够从单击的RecyclerView的子级中检索数据。

public void changeData(){
    final Intent intent = getIntent();
    Log.v("Locationsdsa", intent.getStringExtra("EXTRA_LOCATION"));
    Log.v("LocationsdsaLOLOL", intent.getStringExtra("EXTRA_POSITION"));
    DatabaseReference db = FirebaseDatabase.getInstance().getReference("Data");
    db.child(intent.getStringExtra("EXTRA_LOCATION")).child(intent.getStringExtra("EXTRA_POSITION")).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            placeName = dataSnapshot.child("nameResto").getValue(String.class);
            String restoAddress = dataSnapshot.child("restoAddress").getValue(String.class);
            String restoReview = dataSnapshot.child("restoReviews").getValue(String.class);
            String imgUrl = dataSnapshot.child("restoThumbnail").getValue(String.class);
            langitude = dataSnapshot.child("restoLangitude").getValue(Double.class);
            longitude = dataSnapshot.child("restoLongitude").getValue(Double.class);

            Log.v("langitude", String.valueOf(langitude));
            Log.v("longitude", String.valueOf(longitude));
            Log.v("name", placeName);
            Log.v("restoaddress", restoAddress);
            Log.v("reviewresto", restoReview);
            tvPlace.setText(placeName);
            tvAddress.setText(restoAddress);
            tvRating.setText(restoReview);
            Picasso.get().load(imgUrl).resize(200, 200).centerInside().into(ivPlace);


        }

我希望可以使用ValueEventListener检索数据

1 个答案:

答案 0 :(得分:1)

以下是用户经常错过的一些事情:

  • 缺少Firebase规则,默认为阻止

  • 忘记下载google-services.json

  • 键名与代码中的拼写错误

  • 元帅Java pojo的方法声明错误或缺少set方法

经常被忽略的另一件事是,如果离线功能启用,例如:

  

FirebaseDatabase.getInstance()。setPersistenceEnabled(true);

在调试应用程序时,人们可能会忘记Firebase中更改的数据需要先传播到设备,然后设备才能对更改做出反应。这似乎是在发生,但是如果互联网不畅或其他实习生来了,可能会发生奇怪的事情,使您立即不知道

就像@AlexMamo的Firebase offline capabilities causing memory problems答案一样,讨论也会显示出来。

如果遇到勒索,可以随时卸载应用程序以发布应用程序数据库的本地持久版本。

相关问题