提问人:Rajendra 提问时间:11/12/2023 最后编辑:Rajendra 更新时间:11/14/2023 访问量:68
Firebase Realtime Database 未显示
Firebase Realtime Database not showing
问:
我已经将 Firebase 实时数据库与我的 android 应用程序集成在一起。数据正在控制台上打印。当我在模拟器中运行应用程序时,数据会反映在模拟器中并加载。 但是当我在我的pysical android设备中运行相同的应用程序时,我无法看到来自firebase的数据,并且页面是空白的,尽管数据也在这里的控制台上打印。
我的 Android 清单.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.studydatascience">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
tools:node="remove" />
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.StudyDataScience">
<activity
android:name=".newActivity"
android:exported="false" />
<.. some activities ..>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-1234567891234567~0123456789" />
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.StudyDataScience.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的 Build.gradle 文件
plugin {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.rajendra.studydatascience"
minSdkVersion 21
targetSdkVersion 31
versionCode 12
versionName "2.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.google.ads.mediation:facebook:6.3.0.0'
implementation 'com.google.android.ads:mediation-test-suite:1.5.0'
implementation 'com.facebook.android:audience-network-sdk:6.3.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment:2.3.4'
implementation 'androidx.navigation:navigation-ui:2.3.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.firebase:firebase-database:20.3.0'
testImplementation 'junit:junit:4.13.2'
implementation 'com.google.android.gms:play-services-ads:20.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.17'
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
implementation 'androidx.work:work-runtime-ktx:2.7.1'
implementation 'com.android.volley:volley:1.2.1'
implementation 'com.google.firebase:firebase-core:21.1.1'
}
这是从 firebase 获取数据的代码
public void fetch(String keyValue){
Adapter adapter1 = new Adapter(newActivity.this,arrayList);
recyclerView = findViewById(R.id.view_recycler);
recyclerView.setAdapter(adapter1);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Initialize Firebase
DatabaseReference cartListRef = FirebaseDatabase.getInstance().getReference().child("questions").child(keyValue);
cartListRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
int questionCount = 0;
for (DataSnapshot dataSnapshot:snapshot.getChildren()){
String answer = dataSnapshot.child("answer").getValue(String.class);
String question = dataSnapshot.child("question").getValue(String.class);
answer = answer.replace("\\n", "\n").trim();
arrayList.add(new QA(question,answer));
Log.d("TAG", "onDataChange: "+answer);
}
这些是Firebase实时数据库的规则
示例 Firebase 数据库 json 文件
{
"questions": {
"machine learning": [
{
"answer": "Tensor has three indices where the first points to the row, the second to the column and the third one to the axis",
"question": "What is a Tensor?"
}
],
"python": [
{
"answer": "Different data structures in Python include \n Builtin Data Structures:\n List, Dictionary, Tuple, Set \n UserDefined Data Structure:\n Stack, Queue, Tree, Linked List, Graph, HashMap",
"question": "What are the different data structures in Python?"
}
],
"deep learning": [
{
"answer": "perceptron is a singlelayer neural network. It consists of input value, weight and bias, activation function. Input value is multiplies with weight and added to bias, then activation function is applied to it.",
"question": "What is a perceptron?"
}
]}}
答: 暂无答案
评论