Firebase数据检索(随机密钥)

时间:2018-03-31 08:53:16

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

这里我只是从Firebase检索数据并打印在(Log)中。我需要在ListviewRecyclerView中显示。我提到了很多网站,博客,视频等,但我仍未找到合适的解决方案。

打印的LOG屏幕截图: enter image description here

Firebase结构: enter image description here

Problem video link

我需要解决方案或适当的网站来解决这个问题。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:0)

您需要获取员工个人详细信息的数据。数据是一个字符串,并在下面的代码中传递:

    JSONObject json = new JSONObject(data);
    Iterator<?> keys = json.keys();
    while(keys.hasNext())
    {
        String key = (String)keys.next();
        JSONObject jsonItem = json.getJSONObject(key);

    }

每个项目都有jsonobject。

答案 1 :(得分:0)

您需要为DataSnapshot创建模型类,以便您可以像这样使用。

模特课程:

import com.google.firebase.database.PropertyName;

/**
* Created on 31/3/18.
*/
public class Employee {

@PropertyName("Employee Address")
public long emplyeeAddress;
@PropertyName("Employee Age")
public long emplyeeAge;
@PropertyName("Employee Blood Group")
public long emplyeeBloodGroup;
@PropertyName("Employee Date of joining")
public long emplyeeDOJ;
@PropertyName("Employee Gender")
public long emplyeeGender;
@PropertyName("Employee ID")
public long emplyeeId;
@PropertyName("Employee Married Status")
public long emplyeeMarriedStatus;
@PropertyName("Employee Name")
public long emplyeeName;
@PropertyName("Employee Phone Number")
public long emplyeePhoneNumber;
@PropertyName("Employee Qualification")
public long emplyeeQualification;

String key;

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}

@PropertyName("Employee Address")
public long getEmplyeeAddress() {
    return emplyeeAddress;
}

@PropertyName("Employee Address")
public void setEmplyeeAddress(long emplyeeAddress) {
    this.emplyeeAddress = emplyeeAddress;
}

@PropertyName("Employee Age")
public long getEmplyeeAge() {
    return emplyeeAge;
}

@PropertyName("Employee Age")
public void setEmplyeeAge(long emplyeeAge) {
    this.emplyeeAge = emplyeeAge;
}

@PropertyName("Employee Blood Group")
public long getEmplyeeBloodGroup() {
    return emplyeeBloodGroup;
}

@PropertyName("Employee Blood Group")
public void setEmplyeeBloodGroup(long emplyeeBloodGroup) {
    this.emplyeeBloodGroup = emplyeeBloodGroup;
}

@PropertyName("Employee Date of joining")
public long getEmplyeeDOJ() {
    return emplyeeDOJ;
}

@PropertyName("Employee Date of joining")
public void setEmplyeeDOJ(long emplyeeDOJ) {
    this.emplyeeDOJ = emplyeeDOJ;
}

@PropertyName("Employee Gender")
public long getEmplyeeGender() {
    return emplyeeGender;
}

@PropertyName("Employee Gender")
public void setEmplyeeGender(long emplyeeGender) {
    this.emplyeeGender = emplyeeGender;
}

@PropertyName("Employee ID")
public long getEmplyeeId() {
    return emplyeeId;
}

@PropertyName("Employee ID")
public void setEmplyeeId(long emplyeeId) {
    this.emplyeeId = emplyeeId;
}

@PropertyName("Employee Married Status")
public long getEmplyeeMarriedStatus() {
    return emplyeeMarriedStatus;
}

@PropertyName("Employee Married Status")
public void setEmplyeeMarriedStatus(long emplyeeMarriedStatus) {
    this.emplyeeMarriedStatus = emplyeeMarriedStatus;
}

@PropertyName("Employee Name")
public long getEmplyeeName() {
    return emplyeeName;
}

@PropertyName("Employee Name")
public void setEmplyeeName(long emplyeeName) {
    this.emplyeeName = emplyeeName;
}

@PropertyName("Employee Phone Number")
public long getEmplyeePhoneNumber() {
    return emplyeePhoneNumber;
}

@PropertyName("Employee Phone Number")
public void setEmplyeePhoneNumber(long emplyeePhoneNumber) {
    this.emplyeePhoneNumber = emplyeePhoneNumber;
}

@PropertyName("Employee Qualification")
public long getEmplyeeQualification() {
    return emplyeeQualification;
}

@PropertyName("Employee Qualification")
public void setEmplyeeQualification(long emplyeeQualification) {
    this.emplyeeQualification = emplyeeQualification;
}
}

价值观察者:

private ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot data) {

        List<Employee> employees = new ArrayList<>();

        if (data != null && data.getValue() != null && data.getChildrenCount() > 0) {

            for (DataSnapshot snapShot : data.getChildren()) {

                Employee emplyess = snapShot.getValue(Employee.class);
                emplyess.setKey(snapShot.getKey());
            }

        }
        //set your list to adapter

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};

我希望它能帮到你: - )

注意:可能是我在员工属性名称中犯了一些拼写错误所以请仔细检查

答案 2 :(得分:0)

以下是使用ListViewArrayAdapter中显示员工姓名的方法:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Employee personal detail");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String employeeName = ds.child("Employee Name").getValue(String.class);
            list.add(employeeName);
        }
        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) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);