【问题标题】:Getting Values From Firebase Into A ListView将值从 Firebase 获取到 ListView
【发布时间】:2017-04-27 13:28:55
【问题描述】:

我需要从“出席”节点填充列表视图:

每个列表项都应显示运动、日期和时间。这是我为获取值所做的工作:

        final DatabaseReference attendanceRef = database.getReference()
            .child(Constants.MEMBERS_NODE).child(userID).child("attendance").child(today);

    attendanceRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                String sport = snapshot.getKey();
                String date = (String) snapshot.child("date").getValue();
                String timeStamp = (String) snapshot.child("timestamp").getValue();

            }
        }

请注意,我不能只获取快照的值,因为我需要获取密钥,即运动名称。我不知道如何处理它。感谢您的任何建议

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    创建一个 hashmap 来映射值。

    HashMap<String,String> sportsMap = snapshot.getValue();
    String date = sportsMap.get("date");
    

    【讨论】:

    • 我试过了,但不确定如何使用该哈希映射创建列表适配器。
    【解决方案2】:

    我希望列表项显示运动名称以及参加的时间和日期。答案并不复杂,但需要一点深刻的理解。

    我应该创建一个模型类来获取日期和时间戳:

    public class MemberAttendance {
    
    private String sport;
    private long timestamp;
    
    public MemberAttendance() {
    
    }
    
    public MemberAttendance(String sport, long timestamp) {
        this.sport = sport;
        this.timestamp = timestamp;
    }
    
    public String getSport() {
        return sport;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public void setSport(String sport) {
        this.sport = sport;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
    

    }

    然后像以前一样创建一个引用:

    final DatabaseReference attendanceRef = database.getReference()
                .child(Constants.MEMBERS_NODE).child(userID).child("attendance").child(today);
    

    然后像这样初始化适配器:

            attendanceAdapter = new MemberAttendanceAdapter(getActivity(), MemberAttendance.class,
                R.layout.list_item_attendance, attendanceRef);
    
        attendanceListView.setAdapter(attendanceAdapter);
    

    真正的窍门是在适配器中,我需要让运动等于引用键,即运动名称:

    public class MemberAttendanceAdapter extends FirebaseListAdapter<MemberAttendance> {
    
    public MemberAttendanceAdapter(Activity activity, Class<MemberAttendance> modelClass, int modelLayout, Query ref) {
        super(activity, modelClass, modelLayout, ref);
    }
    
    @Override
    protected void populateView(View view, MemberAttendance memberAttendance, int i) {
    
        String sport = getRef(i).getKey();
    
        // Create views and assign values
        TextView sportTxtView = (TextView) view.findViewById(R.id.sportTxtView);
        sportTxtView.setText(sport);
    
        // This time stamp can return date and time, so there is no need to create a date getter
        Date timestamp = new Date(memberAttendance.getTimestamp());
    
        TextView dayTxtView = (TextView) view.findViewById(R.id.dayTxtView);
        dayTxtView.setText(new SimpleDateFormat("dd-MM-yyyy").format(timestamp));
    
        // Show the time
        TextView dateTxtView = (TextView) view.findViewById(R.id.dateTxtView);
        dateTxtView.setText(new SimpleDateFormat("hh:mm a").format(timestamp));
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2018-10-14
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多