在ListView中显示Firebase数据

时间:2018-02-05 12:21:23

标签: java android listview firebase firebase-realtime-database

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

   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 + '\'' +
            '}';
}
 }

数据库: Link to my database screenshot

2 个答案:

答案 0 :(得分:1)

这是异步API的经典问题。为了使其工作,请根据Java Naming Conventions更改模型类。你的课应该是这样的:

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类的对象添加到列表之前就会调用它。换句话说,您的list在该方法之外将始终为空。快速解决方法是在onDataChange()内移动列表声明并执行您想要的操作,或者,如果您想深入了解异步世界并使用我的post答案。

假设score节点是Firebase根目录的直接子节点,要使用String类显示数据,请使用以下代码:

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);

这就是如何使用Score类显示数据。

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);

答案 1 :(得分:0)

filepath.addOnSuccessListener(upload data first) then get it and use it....