在 Android 活动中从 Firebase 检索数据

我阅读了许多关于如何从 Firebase 数据库实例获取数据的线程,但没有一个对我有用。


我在活动中的代码:


public class Violations extends AppCompatActivity

{

TextView textView7;


FirebaseDatabase database = FirebaseDatabase.getInstance();

DatabaseReference dbref = database.getReference("save");


@Override

protected void onCreate(Bundle savedInstanceState)

{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_violations);


    textView7 = findViewById(R.id.textView7);


    dbref.addValueEventListener(new ValueEventListener()

    {

        ArrayList<String> Violations = new ArrayList<>();

        @Override

        public void onDataChange(DataSnapshot dataSnapshot)

        {

            for (DataSnapshot postSnapshot: dataSnapshot.getChildren())

            {

                Violations.add(postSnapshot.getValue().toString());

                System.out.println(postSnapshot.getValue().toString());

            }

            for(int i=0; i < Violations.size(); i++)

            {

                textView7.setText(textView7.getText() + Violations.get(i) + System.lineSeparator());

            }

        }


        @Override

        public void onCancelled(DatabaseError databaseError) {}

    });

}

}

那里没有错误,但没有数据显示。我很确定,我的问题是连接到正确的实例并检索数据。


Firebase 数据,是这样的:

http://img3.mukewang.com/615421c50001602007680723.jpg

MYYA
浏览 158回答 3
3回答

斯蒂芬大帝

初始化 Firebase 数据库和DatabaseReference内部onCreate()方法:@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_violations);&nbsp; &nbsp; FirebaseDatabase database = FirebaseDatabase.getInstance();&nbsp; &nbsp; DatabaseReference dbref = database.getReference("save");&nbsp; &nbsp; ...&nbsp; &nbsp; ..此外,您获得的参考是save但 Firebase 数据库显示为 : you violate ...。您可以尝试将名称更改为save或获取正确的数据 : you violate ...。

慕容3067478

尝试使用addListenerForSingleValue()而不是addValueEventListener().&nbsp;希望能帮助到你。并将您的 firebase 链接更改save为you violate your own speed limit with

明月笑刀无情

我在您的数据库架构中看不到 o 已命名的引用,save但我看到了一个已命名的You violate your own speed limit with,必须在您的引用中使用它才能从数据库中获取数据。因此,要解决此问题,请使用以下代码行:DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();DatabaseReference ref = rootRef.child("You violate your own speed limit with");ValueEventListener valueEventListener = new ValueEventListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> violations = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for(DataSnapshot ds : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String speed = ds.getValue(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; violations.add(speed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, speed);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Do what you need to do with y our violations list&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, databaseError.getMessage());&nbsp; &nbsp; }};ref.addListenerForSingleValueEvent(valueEventListener);logcat 中的结果将是所有这些值:3 km / h at time: ...3 km / h at time: ...//an so on
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java