我正在尝试将Android应用连接到Firebase实时数据库,并在数据库中读取和写入数据。
我已经阅读了很多教程,该教程甚至是关于Firebase本身的YouTube视频“ Android上的Firebase实时数据库入门”。关于堆栈溢出,这里有很多关于此问题的主题,我确实尝试了发现的几乎所有解决方案。他们都没有为我工作。总结一下我在发布此文章之前已经看过的要点:
我的build.gradle(模块):
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.google.gms:google-services:4.2.0'
}
我的build.gradle(项目)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-database:18.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.0.0'
}
apply plugin: 'com.google.gms.google-services'
我用于初始化Firebase和实现addValueListener的Java语法
package com.example.scratch;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class CinemaAndOthersActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<SampleItem> itemList;
public DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference();
public DatabaseReference cinemasRef = myRootRef.child("Cinema");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cinema_and_others);
itemList = new ArrayList<>();
itemList.add(new SampleItem(R.drawable.ic_launcher_background, "Item1 - line1", "Item1 - line1"));
itemList.add(new SampleItem(R.drawable.ic_launcher_foreground, "Item2 - line2", "Item2 - line2"));
for(SampleItem Rar:itemList ){
Log.d("DEBUG", Rar.getmLine1());
}
mRecyclerView = findViewById(R.id.recycleView);
mRecyclerView.setHasFixedSize(true);
mAdapter = new SampleAdapter(itemList);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(mLayoutManager);
}
@Override
protected void onStart() {
super.onStart();
Log.d("DEBUG", "This is right before onDataChange method.");
cinemasRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
Log.d("DEBUG", "My cinema is: "+text+"!!!!!!!!!!!!!!!!!!!!!!");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
我的Firebase实时数据库
{
"Cinema" : "Arena"
}
我的Firebase实时数据库规则
{
"rules": {
".read": true,
".write": true
}
}
我的google-services.json
我的Android.Manifest.xml具有INTERNET权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.scratch">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CinemaAndOthersActivity"
android:label="@string/cinema"
android:parentActivityName=".MainActivity"
android:theme="@style/Activity2Theme">
</activity>
</application>
</manifest>
在我的logcat中,我收到消息:
“ 2019-07-17 22:50:36.290 30164-30164 / com.example.scratch I / FirebaseInitProvider:FirebaseApp初始化成功”
当相关活动开始时,将显示以下内容:
“ 2019-07-17 22:50:57.312 30164-30164 / com.example.scratch D / DEBUG:就在onDataChange方法之前。 “
但是我的Log.d("DEBUG", "My cinema is: "+text+"!!!!!!!!!!!!!!!!!!!!!!");
永远不会被看到...
我的理解是,此方法应在开始时以及每次在所引用节点中进行更改时调用一次。都没有发生。我想念什么?任何人都可以建议我,因为对我来说这似乎是安装问题,而不是Java实现问题?
EDiT#1用代码段替换所有链接。