gpt4 book ai didi

android - 从 firebase 检索多个数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:41:33 27 4
gpt4 key购买 nike

嘿,我对 firebase 和编程也比较陌生,所以如果这是一个愚蠢的问题,我很抱歉。我正在努力检索食谱

Imgur

上面是来自 firebase 的数据树,下面是来自 MainActivity 的代码

       package com.example.myapplicationdatatest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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;

public class MainActivity extends AppCompatActivity {

private TextView category;
private TextView cookinTime;
private TextView description;
private TextView ingridients;
private TextView nameOfRecipe;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
category = findViewById( R.id.category );
cookinTime = findViewById( R.id.cookingTime );
description = findViewById( R.id.description );
ingridients = findViewById( R.id.ingridients );
nameOfRecipe = findViewById( R.id.nameOfRecipe );
FirebaseApp.initializeApp( getApplicationContext() );






FirebaseDatabase.getInstance().getReference().child( "recipes" )
.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Recipe recipe = snapshot.getValue( Recipe.class );

category.setText( recipe.getCategory() );
cookinTime.setText( recipe.getCookingtime() );
description.setText( recipe.getDescription() );
ingridients.setText( recipe.getIngredients() );
nameOfRecipe.setText( recipe.getIngredients() );
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}





}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>

<TextView
android:id="@+id/cookingTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/ingridients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/nameOfRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

配方类

    package com.example.myapplicationdatatest;

public class Recipe {

private String category;
private String cookingtime;
private String description;
private String ingredients;
private String nameOfRecipe;

public Recipe() {};

public Recipe(String category, String cookingtime, String description,String ingredients, String nameOfRecipe) {
this.category=category;
this.cookingtime=cookingtime;
this.description=description;
this.ingredients=ingredients;
this.nameOfRecipe=nameOfRecipe;
}

public String getCategory() {
return category;
}

public String getCookingtime() {
return cookingtime;
}

public String getDescription() {
return description;
}

public String getIngredients() {
return ingredients;
}

public String getNameOfRecipe() {
return nameOfRecipe;
}
}

和异常

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplicationdatatest, PID: 6400
com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@19.1.0:408)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.1.0:199)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@19.1.0:178)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@19.1.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.1.0:586)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.1.0:545)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.1.0:415)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.1.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.1.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.1.0:203)
at com.example.myapplicationdatatest.MainActivity$1.onDataChange(MainActivity.java:53)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.1.0:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.1.0:55)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我真的不知道如何检索这些多个分支,尤其是因为我想添加多个食谱

谢谢你的帮助

最佳答案

您收到以下错误:

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String

因为您的 Recipe 中字段的类型类与数据库中的属性类型不同。你有你的 Recipe对名为 ingredients 的字段进行分类这是类型 String在数据库中我看到这是一个 Map<String, Object>这是正确的,因此是错误的。字段的类型必须匹配。除此之外,您的数据库中还有一个名为 amountOfIngredients 的属性。你的 Recipe 中实际上缺少它类(class)。要解决此问题,请为您的 Recipe 建模根据数据库中存在的属性类型进行分类,您的问题将得到解决。

关于android - 从 firebase 检索多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com