无法从我的Firebase数据库中打印所有子项

时间:2020-07-06 22:27:01

标签: android firebase firebase-realtime-database android-recyclerview android-adapter

Output of all the dates snippet of my firebase database

我想从我的数据库中打印7月5日至7月7日的所有日期(子代)详细信息,但是我只得到最高日期(子代)[7月7日]作为我的输出,而不是所有子节点** / p>

这是我的参考,我从数据库中获取值并将其放入RecyclerView适配器

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Dateinfo").child(firebaseUser.getUid());
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mdates.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        Dateinfo dateinfo = snapshot.getValue(Dateinfo.class);
                        mdates.add(dateinfo);
                    }
                    Collections.reverse(mdates);
                    dateAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }

这是我的适配器

public class DateAdapter extends RecyclerView.Adapter<DateAdapter.ViewHolder> {
private Context mContext;
private List<Dateinfo> mdates;
FirebaseUser firebaseUser;

public DateAdapter(Context mContext, List<Dateinfo> mdates) {
    this.mContext = mContext;
    this.mdates = mdates;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.date_item,parent,false);
    return new DateAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
    final Dateinfo dateinfo =mdates.get(position);
    holder.date.setText(dateinfo.getDate());
    holder.steps.setText(String.valueOf(dateinfo.getSteps()));
    String timeformat = String.format(Locale.getDefault(),"%.2f", dateinfo.getDistance());
    holder.distance.setText(timeformat);
}

@Override
public int getItemCount() {
    return Math.min(mdates.size(),10);
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView date;
    public TextView steps,distance;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        date=itemView.findViewById(R.id.dateview);
        steps=itemView.findViewById(R.id.steps);
        distance=itemView.findViewById(R.id.distance);
    }
}

这是我的Dateinfo类:

public class Dateinfo {
    long steps;
    String ID;
    String date;
    double distance;

    public Dateinfo() {
    }

    public Dateinfo(long steps, String ID, String date, double distance) {
        this.steps = steps;
        this.ID=ID;
        this.date=date;
        this.distance=distance;

    }
    public double getDistance() {
        return distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public long getSteps() {
        return steps;
    }

    public void setSteps(long steps) {
        this.steps = steps;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }



    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }
}

0 个答案:

没有答案