gpt4 book ai didi

java - 如何从 Firebase 检索数据并将其设置为图表的 string 和 int

转载 作者:行者123 更新时间:2023-12-02 08:50:51 25 4
gpt4 key购买 nike

早上好/下午/晚上好,我只需要帮助如何将字符串和值的数据从 Firebase 更改为我的代码或图表。我目前正在使用如图所示的预设值(字符串“skyobject”和int“500,300,600,123”,并希望使用图像中显示的我的firebase中的数据。谢谢你帮助我,我是这个android studio的新手,所以我非常感谢我能得到的任何帮助。提前非常感谢那些帮助我的人

public class MainActivity extends AppCompatActivity {
AnyChartView anyChartView;
Button btn;
DatabaseReference reff;


String [] skyobject = {"sun","moon","stars","planets"};
int[] datas = {500,300,600,123};


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

anyChartView =( AnyChartView) findViewById(R.id.any_chart_view);
setupPieChart();


}
public void setupPieChart() {
Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();

for (int i=0;i<skyobject.length;i++){
dataEntries.add(new ValueDataEntry(skyobject[i],datas[i]));


}
pie.data(dataEntries);
anyChartView.setChart(pie);

}
}

使用@Alex Mamo的给定代码编辑代码

package com.javapapers.androidchartapp;

import android.os.Bundle;
import android.util.Log;

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

import com.anychart.AnyChart;
import com.anychart.AnyChartView;
import com.anychart.chart.common.dataentry.DataEntry;
import com.anychart.chart.common.dataentry.ValueDataEntry;
import com.anychart.charts.Pie;
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 com.javapapers.androidchartapp.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
AnyChartView anyChartView;

DatabaseReference rootRef;


String [] skyobject = {"sun","moon","stars","planets"};
int[] datas = {500,300,600,123};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String name = ds.getValue(String.class);
Log.d("TAG", key + ": " + name);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
rootRef.addListenerForSingleValueEvent(valueEventListener);
anyChartView = findViewById(R.id.any_chart_view);
setupPieChart();


}
public void setupPieChart() {
Pie pie = AnyChart.pie();
List<DataEntry> dataEntries = new ArrayList<>();

for (int i=0;i<skyobject.length;i++){
dataEntries.add(new ValueDataEntry(skyobject[i],datas[i]));

}
pie.data(dataEntries);
anyChartView.setChart(pie);

}

这是Logcat中的错误



--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.javapapers.androidchartapp, PID: 22809
java.lang.RuntimeException: Uncaught exception in Firebase Database runloop (3.0.0). Please report to firebase-database-client@google.com
at com.google.firebase.database.android.AndroidPlatform$1$1.run(com.google.firebase:firebase-database@@16.0.4:98)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/FirebaseApp$IdTokenListener;
at com.google.firebase.database.android.AndroidPlatform.newAuthTokenProvider(com.google.firebase:firebase-database@@16.0.4:112)
at com.google.firebase.database.core.Context.ensureAuthTokenProvider(com.google.firebase:firebase-database@@16.0.4:246)
at com.google.firebase.database.core.Context.initServices(com.google.firebase:firebase-database@@16.0.4:98)
at com.google.firebase.database.core.Context.freeze(com.google.firebase:firebase-database@@16.0.4:77)
at com.google.firebase.database.core.RepoManager.createLocalRepo(com.google.firebase:firebase-database@@16.0.4:92)
at com.google.firebase.database.core.RepoManager.createRepo(com.google.firebase:firebase-database@@16.0.4:42)
at com.google.firebase.database.FirebaseDatabase.ensureRepo(com.google.firebase:firebase-database@@16.0.4:357)
at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database@@16.0.4:201)
at com.javapapers.androidchartapp.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.FirebaseApp$IdTokenListener" on path: DexPathList[[zip file "/data/app/com.javapapers.androidchartapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.javapapers.androidchartapp-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.google.firebase.database.android.AndroidPlatform.newAuthTokenProvider(com.google.firebase:firebase-database@@16.0.4:112) 
at com.google.firebase.database.core.Context.ensureAuthTokenProvider(com.google.firebase:firebase-database@@16.0.4:246) 
at com.google.firebase.database.core.Context.initServices(com.google.firebase:firebase-database@@16.0.4:98) 
at com.google.firebase.database.core.Context.freeze(com.google.firebase:firebase-database@@16.0.4:77) 
at com.google.firebase.database.core.RepoManager.createLocalRepo(com.google.firebase:firebase-database@@16.0.4:92) 
at com.google.firebase.database.core.RepoManager.createRepo(com.google.firebase:firebase-database@@16.0.4:42) 
at com.google.firebase.database.FirebaseDatabase.ensureRepo(com.google.firebase:firebase-database@@16.0.4:357) 
at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database@@16.0.4:201) 
at com.javapapers.androidchartapp.MainActivity.onCreate(MainActivity.java:42) 
at android.app.Activity.performCreate(Activity.java:6955) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

我的数据库图像click here

饼图输出的图像 click here

用给定的代码进行编辑,出现标签 click here 错误

最佳答案

要获取这些属性的键和值,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String name = ds.getValue(String.class);
Log.d("TAG", key + ": " + name);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
rootRef.addListenerForSingleValueEvent(valueEventListener);

logcat 中的结果将是:

Moon: 300
Planets: 123
Stars: 600
Sun: 500

关于java - 如何从 Firebase 检索数据并将其设置为图表的 string 和 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60785590/

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