在ListView中显示Firebase数据 [英] Showing Firebase data in ListView

查看:169
本文介绍了在ListView中显示Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我让它运行显示用户ID但没有得分。然后我开始做一些改变,忘了我改变了什么,现在我又回到了null null。我觉得我可能删除了某些东西或拼写错误。

Okay so I got it to run showing the User ID but not score. I then began making some changes, forgot what I'd changed and now I'm back to null null again. I feel like I may have deleted something or misspelled something.

   dbref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                ArrayList<String> list = new ArrayList<>();
                list.clear();

                for(DataSnapshot ds :dataSnapshot.getChildren()) {
                    Score Result = ds.getValue(Score.class);
                    String userId = String.valueOf(Result.getUserId());
                    String score = String.valueOf(Result.getScore());
                    list.add(userId);
                    list.add(score);

                }
                adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list);
                LvRanking.setAdapter(adapter);
            }

这是我的模特:

public class Score {
private String userId, score;

public Score() {}

public Score(String userId, String score) {
    this.userId = userId;
    this.score = score;
}

public String getUserId() {
    return userId;
}

public String getScore() {
    return score;
}

@Override
public String toString() {
    return "Score{" +
            "userId='" + userId + '\'' +
            ", score='" + score + '\'' +
            '}';
}
 }

数据库:
链接到我的数据库屏幕截图

推荐答案

这是异步API的经典问题。为了使其工作,请根据 Java命名约定更改模型类。你的课程应如下所示:

This is a classic issue with asynchronous APIs. In order to make it work, change your model class acording to Java Naming Conventions. You class should look like this:

public class Score {
    private String userId, score;

    public Score() {}

    public Score(String userId, String score) {
        this.userId = userId;
        this.score = score;
    }

    public String getUserId() {
        return userId;
    }

    public String getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Score{" +
                "userId='" + userId + '\'' +
                ", score='" + score + '\'' +
                '}';
    }
}

另请注意 onDataChange( )方法具有异步行为,这意味着甚至在您尝试将 Score 类的对象添加到列表之前就会调用它。换句话说,您的列表在该方法之外将始终为空。快速解决方法是在 onDataChange()中移动列表声明并执行您要对其执行的操作,或者,如果您想深入了解异步世界,使用我的答案发布

Also note that onDataChange() method has an asynchronous behaviour, which means that is called even before you are trying to add those objects of Score class to the list. With other words, your list will always be empty outside that method. A quick fix would be to move the declaration of your list inside onDataChange() and do what you want to do with it or, if you want to dive into the asynchronous world and use my answer from this post.

假设得分节点是Firebase root的直接子节点,则使用以下内容显示数据String类,请使用以下代码:

Assuming the score node is a direct child of your Firebase root, to display the data using the String class, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String userId = ds.child("userId").getValue(String.class);
            String score = ds.child("score").getValue(String.class);
            list.add(userId + " / " +  score);
            Log.d("TAG", userId + " / " +  score);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);

这是如何使用分数显示数据 class。

And this how you can display data using Score class.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference scoreRef = rootRef.child("score");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Score> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Score s = ds.getValue(Score.class);
            String userId = s.getUserId();
            String score = s.getScore();
            Log.d("TAG", userId + " / " +  score);
            list.add(score);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
scoreRef.addListenerForSingleValueEvent(eventListener);

这篇关于在ListView中显示Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆