从firebase数据库中检索特定数据

时间:2018-05-05 11:15:05

标签: android firebase firebase-realtime-database

我正在尝试从firebase数据库中检索数据。这是我数据库的结构

database structure image

我想获取引用XqU1a6jrvKSLf4ulhI3LkOhChE92和子ahmad的数据,并在textView中显示bikingTime,但我得到0。我在MainActivity

中写了这段代码
child mychild=new child();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseDatabase database= FirebaseDatabase.getInstance();

    String name="ahmad";
    DatabaseReference myRef = database.getReference("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);

    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {



            mychild = dataSnapshot.getValue(child.class);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    myRef.addValueEventListener(listener);
    test = (TextView) findViewById(R.id.test);
    test.setText(String.valueOf(mychild.getBikingTime()));
}

这是child类:

public class child {
public int bikingTime=0;
public int noOfKicks=0;
public int noOfJumps=0;


public child() {
}

public child(int bikingTime, int noOfKicks, int noOfJumps) {
    this.bikingTime = bikingTime;
    this.noOfKicks = noOfKicks;
    this.noOfJumps = noOfJumps;
}

public int getBikingTime() {
    return bikingTime;
}

public void setBikingTime(int bikingTime) {
    this.bikingTime = bikingTime;
}

public int getNoOfKicks() {
    return noOfKicks;
}

public void setNoOfKicks(int noOfKicks) {
    this.noOfKicks = noOfKicks;
}

public int getNoOfJumps() {
    return noOfJumps;
}

public void setNoOfJumps(int noOfJumps) {
    this.noOfJumps = noOfJumps;
}

2 个答案:

答案 0 :(得分:0)

更改这些内容将根节点添加到查询中..

        DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("child-activity-kit").child("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);


  DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
    Query query=myRef.child("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mychild = dataSnapshot.getValue(child.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 1 :(得分:0)

您可以按照以下方式更改代码。它应该工作。

child mychild=new child();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        test = (TextView) findViewById(R.id.test);

        FirebaseDatabase database= FirebaseDatabase.getInstance();

        String name="ahmad";
        DatabaseReference myRef = database.getReference().child("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);

        ValueEventListener listener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                mychild = dataSnapshot.getValue(child.class);
                test.setText(String.valueOf(mychild.getBikingTime()));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        };
        myRef.addValueEventListener(listener);


    }