从firebase检索数据时的空值

时间:2018-02-05 23:43:26

标签: java android firebase firebase-realtime-database

enter image description here我有一个包含多个用户的考勤应用程序,我正在尝试检索并显示一些用户详细信息到列表名称/姓氏/ empId。执行代码时,它会检索并显示名字,但不显示其他2个值。调试时,值显示为NULL,我无法看到我错过了什么。希望有些新鲜的眼睛可以提供帮助

任何帮助真的很感激 - 下面带有图片的代码

[//Activity code to get data

protected void onStart() {
    super.onStart();
    databaseOut.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            userList.clear();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                User user = postSnapshot.getValue(User.class);                   
                userList.add(user);

            }
            zz_List_Users userListAdapter = new zz_List_Users(updateEmployeeDetailsActivity.this, userList);
            listViewUsers.setAdapter(userListAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}][1]

//我希望检索并添加到列表中的用户列表数据

public class zz_List_Users extends ArrayAdapter<User> {
    private Activity context;
    List<User> userList;

    public zz_List_Users(Activity context, List<User> userList) {
        super(context, R.layout.zz_list_user, userList);
        this.context = context;
        this.userList = userList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View listViewItem = inflater.inflate(R.layout.zz_list_user, null, true);

        TextView textViewNameU = (TextView) listViewItem.findViewById(R.id.textViewNameU);
        TextView textViewSurname = (TextView) listViewItem.findViewById(R.id.textViewSurnameU);
        TextView textViewEmpId = (TextView) listViewItem.findViewById(R.id.textViewEmpIdU);

        User user = userList.get(position);

        textViewSurname.setText(user.getSurname());
        textViewEmpId.setText(user.getEmpId());
        textViewNameU.setText(user.getName());


        return listViewItem;
    }
}

//用户类

public class User {
    private String userId;
    private String name;
    private String surname;
    private String empId;
    private String password;
    private String address;
    private String postCode;
    private String userEmail;
    public boolean adminUser;


    public User(){

    }

    public User(String userId, String name, String surname, String empId, String password, String address, String postCode, String userEmail, boolean adminUser) {
        this.userId = userId;
        this.name = name;
        this.surname = surname;
        this.empId = empId;
        this.password = password;
        this.address =  address;
        this.postCode =  postCode;
        this.userEmail =  userEmail;
        this.adminUser = adminUser;


    }

    public String getUserId() {
        return userId;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getEmpId() { return empId; }

    public String getPassword() {
        return password;
    }

    public String getAddress() { return address;    }

    public String getPostCode() {return postCode;    }

    public String getUserEmail() {return userEmail;    }

    public boolean getAdminUser() {return adminUser;    }
}

任何帮助真的很感激

//Firebase JSON

{
  "users" : {
    "1mXbSGCzGmbCp7lLXjxjX2kihHI3" : {
      "address" : "31 moyola drive shantallow",
      "adminUser" : true,
      "employeeId" : "902488",
      "firstName" : "Sarah",
      "password" : "solskjaer",
      "postCode" : "bt48 0GE",
      "surname" : "Breslin",
      "userEmail" : "sarahbreslin81@hotmail.com",
      "userId" : "1mXbSGCzGmbCp7lLXjxjX2kihHI3"
    },
    "O0MYu0YmENgodYSHU0BkMSFQCFj2" : {
      "address" : "123 Elaghmore Park",
      "adminUser" : true,
      "employeeId" : "902499",
      "firstName" : "Seamus",
      "password" : "solskjaer",
      "postCode" : "BT48 8DY",
      "surname" : "Ferry",
      "userEmail" : "seamyferry@hotmail.co.uk",
      "userId" : "O0MYu0YmENgodYSHU0BkMSFQCFj2"
    }
  }
}

2 个答案:

答案 0 :(得分:0)

您的字段必须与firebase响应中的字段名称相同。

所以在User班级中将name更改为firstName,就像您的回复一样。还empIdemployeeId

其他领域也一样。

所以请使用此tool生成 POJO 以避免此类问题。

Android工作室中也有一个有用的plugin

答案 1 :(得分:-1)

您在用户类中的字段必须与Firebase中的字段相同。firstName和employeeId是ur json响应中的字段。但是在users类中,您给了name和empid,因此将它们替换为firstName和employeeId。用户类的名称必须与firebase中的名称相同