【问题标题】:Retrieving and displaying data on android studio using Firebase [duplicate]使用 Firebase 在 android studio 上检索和显示数据 [重复]
【发布时间】:2020-02-16 20:15:26
【问题描述】:

我已将数据发送到键“Bench”下的 Firebase 数据库,我正在尝试通过创建 arrayadapter 将数据检索到 android studio:

    public class ScoreInfoAdapter extends ArrayAdapter<ScoreProfile> {

    private Activity context;
    private List<ScoreProfile> scoreList;

    public ScoreInfoAdapter(Activity context, List<ScoreProfile>scoreList){

    super(context,R.layout.list_view,scoreList);
    this.context =context;
    this.scoreList = scoreList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
    View listview = inflater.inflate(R.layout.list_view, null, true);

    TextView Scorename = (TextView)listview.findViewById(R.id.Name_bench);
    TextView Scorelevel = (TextView)listview.findViewById(R.id.Score_of_bench);
    ScoreProfile scoreProfile = scoreList.get(position);

    Scorename.setText(scoreProfile.getUserName());
    Scorelevel.setText(scoreProfile.getUserScore());

    return listview;
    }
    }

我创建了一个带有两个文本视图的列表视图的布局资源文件。 “名称”、“分数”。我创建了一个带有 xml 布局的类,以使用列表视图显示信息,并且在此活动的类上我使用此代码检索数据:

    private FirebaseAuth firebaseAuth;
    private ListView listView;
    DatabaseReference databaseReference;
    private FirebaseDatabase firebaseDatabase;
    List<ScoreProfile> scoreList;


    @Override
    protected void onStart() {
    super.onStart();
    databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

    for(DataSnapshot scoreSnapshot : dataSnapshot.getChildren()){

    ScoreProfile scoreProfile = scoreSnapshot.getValue(ScoreProfile.class);
    scoreList.add(scoreProfile);

    }
    ScoreInfoAdapter scoreInfoAdapter = new ScoreInfoAdapter(RankT.this, scoreList);
    listView.setAdapter(scoreInfoAdapter);

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
    });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rank_t);
    Toolbar toolbar = findViewById(R.id.toolbarMain);
    toolbar.setTitle("GymTastic Rank Table");


    listView = findViewById(R.id.listview);

    firebaseDatabase = FirebaseDatabase.getInstance();

    databaseReference = firebaseDatabase.getReference("Bench");



    scoreList = new ArrayList<>();

    firebaseAuth = FirebaseAuth.getInstance();

但是,当我运行应用程序时,我得到了这个错误:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gymtastic, PID: 14505
    com.google.firebase.database.DatabaseException: Class com.example.gymtastic.ScoreProfile does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.0:569)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.0:562)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.2.0:432)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.0:231)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.0:203)
        at com.example.gymtastic.RankT$1.onDataChange(RankT.java:52)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.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)

我可以做些什么来防止这个错误并成功呈现从数据库中提取的数据。

【问题讨论】:

  • 如果你搜索错误信息,你会发现很多以前的问题,以及如何解决它:google.com/…

标签: android firebase firebase-realtime-database database-connection


【解决方案1】:

先生,此错误表明您在 ScoreProfile 类中没有默认构造函数。您需要在 scoreProfile=file 类中初始化默认构造函数。 public ScoreProfile(){} 因为对于像这样的映射数据 ScoreProfile scoreProfile = scoreSnapshot.getValue(ScoreProfile.class); firebase 需要模态类中的默认构造函数

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 2015-12-08
    • 2023-03-05
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多