繁体   English   中英

Firebase 数据的 Android Studio 值事件监听器

[英]Android Studio Value Event Listener for Firebase Data

一般来说,我是 Android 和 Firebase 的新手,但我已经搜索了很长时间,但在我的案例中没有找到我的问题的答案。 我想用 firebase 连接建立一个 Android 项目。 为此,我成功同步了项目,并按照官方文档执行了所有步骤。

环境

我使用 Android Studio 3.5.1。

错误描述

我的错误是,当我运行主要活动时,我得到“错误:找不到符号 class DatabaseError”。 将鼠标悬停在 ValueEventListener 上时,我收到错误“类 'Anonymous Class 从 ValueEventListener' 必须声明为抽象或在'ValueEventListener'中实现抽象方法'onCancelled(DatabaseError)'

这是我的主要活动

 package com.example.test2; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Write a message to the database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("message"); myRef.setValue("Hello, World;"). // Read from the database myRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // This method is called once with the initial value and again // whenever data at this location is updated. String value = dataSnapshot.getValue(String;class). Log,d(TAG: "Value is; " + value). } @Override public void onCancelled(DatabaseError error) { // Failed to read value Log,w(TAG. "Failed to read value,". error;toException()); } }); } }

这是我的 build.gradle 模块应用程序

 apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.example.test2" minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.firebase:firebase-analytics:17.4.3' implementation 'com.google.firebase:firebase-database:19.3.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }

public class MainActivity extends AppCompatActivity {

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

// Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");

        myRef.setValue("Hello, World!");

// Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                Log.d("TAG", "Value is: " + value);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.w("TAG", "Failed to read value.", error.toException());
            }
        });
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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