Firebase并非始终都获取所有数据

时间:2019-02-17 19:09:26

标签: android firebase firebase-realtime-database

代码

我有以下代码来填充ViewHolder

@Override
            protected void populateViewHolder(RankingViewHolder viewHolder, final Ranking model, int position) {
                final Context mContext = getActivity().getApplicationContext();

                viewHolder.txt_name.setText(model.getUserName());
                viewHolder.txt_score.setText(String.valueOf(model.getScore()));
                viewHolder.txt_class.setText(model.getUser_class());
                Picasso.with(mContext).load(model.getAvatarUrl()).error(R.drawable.ic_people_black_24dp).into(viewHolder.personPhoto);


                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        Intent scoreDetail = new Intent(getActivity(), ScoreDetail.class);
                        scoreDetail.putExtra("viewUser", model.getUserName());
                        startActivity(scoreDetail);

                    }
                });

            }
        };

RankViewHolder代码为:

public class RankingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ItemClickListener itemClickListener;

    public TextView txt_name, txt_score, txt_class;

    public ImageView personPhoto;

    public RankingViewHolder(View itemView) {
        super(itemView);
        txt_name = itemView.findViewById(R.id.txt_name);
        txt_score = itemView.findViewById(R.id.txt_score);
        personPhoto = itemView.findViewById(R.id.person_photo);
        txt_class = itemView.findViewById(R.id.txt_class);
        itemView.setOnClickListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view , getAdapterPosition(),false);

    }
}

问题:

由于某些原因,代码工作正常,但有时我会获取所有数据,有时没有图片,也没有class_text。数据来自firebase,因此我的代码正常工作时,我猜想它与Firebase有关。

有没有办法获取我需要的所有数据?如果需要其他代码,请问,但如果是代码问题,则根本不起作用。 enter image description here

根据要求提供的其他信息

排名模型

public class Ranking {

    private String userName;
    private long score;
    private String avatarUrl;
    private String user_class;

    public Ranking(String userName, long score, String avatarUrl, String user_class) {
        this.userName = userName;
        this.score = score;
        this.avatarUrl = avatarUrl;
        this.user_class = user_class;
    }

    public Ranking() {

    }

    public Ranking(String userName, long score) {
        this.userName = userName;
        this.score = score;
    }

    public Ranking(String userName, long score, String avatarUrl) {
        this.userName = userName;
        this.score = score;
        this.avatarUrl = avatarUrl;
    }

    public String getUser_class() {
        return user_class;
    }

    public void setUser_class(String user_class) {
        this.user_class = user_class;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public long getScore() {
        return score;
    }

    public void setScore(long score) {
        this.score = score;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }
}

Firebase结构:

"anny" : {
      "avatar" : "https://i.imgur.com/5dwlAI2.png",
      "avatarUrl" : "https://i.imgur.com/5dwlAI2.png",
      "score" : 470,
      "userName" : "anny",
      "user_class" : "Geography"
    },

0 个答案:

没有答案