从google firebase检索数据并在android studio app的列表视图中显示

时间:2018-04-23 16:06:45

标签: java android firebase firebase-realtime-database

我和我的团队正在开发一个Android工作室项目。我们可以向firebase添加数据。我们正在尝试从数据库中检索数据,但我们认为我们无法引用正确的ID来检索列表视图中的数据。我们正在尝试检索数量,类型并从Feedings类开始。 This is how our firebase database is set up.

这是我的代码:

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

    mListView = (ListView) findViewById(R.id.listView);

    mAuth = FirebaseAuth.getInstance();
    database = FirebaseDatabase.getInstance();
    //myRef = database.getReference();
    FirebaseUser user = mAuth.getCurrentUser();
    userID = user.getUid();
    myRef = database.getReference("/Users/" + userID + "/Feedings");

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user != null){
                Log.d(TAG, "Signed in" + user.getUid());
                toastMessage("Successfully signed in: ");
            }else{
                Log.d(TAG, "Currently signed out");
            }

        }
    };

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private void showData(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds: dataSnapshot.getChildren()){
        Feeding feeding = new Feeding();
        feeding.setAmount(ds.child(userID).getValue(Feeding.class).getAmount());

        Log.d(TAG, "showData: name: " + feeding.getAmount());

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(feeding.getAmount());
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);
    }
}

@Override
public void onStart(){
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop(){
    super.onStop();
    if(mAuthListener != null){
        mAuth.removeAuthStateListener(mAuthListener);
    }
}


private void toastMessage(String message){
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

}

1 个答案:

答案 0 :(得分:0)

尝试使用官方文档中提供的此类引用。 并注意你的ShowData方法不正确。

myRef = database.getReference();  

  myRef.child("Users").child(userID).child("Feedings")
   .getReference().addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });




private void showData(DataSnapshot dataSnapshot) {
    ArrayList<String> arrayList = new ArrayList<>();

    for(DataSnapshot ds: dataSnapshot.getChildren()){
        Feeding feeding = ds.getValue(Feeding.class);
        Log.d(TAG, "showData: name: " + feeding.getAmount());
        arrayList.add(feeding.getAmount());

    }


        ArrayAdapter adapter = new 
        ArrayAdapter(this,android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);
}