[英]Retrieve data from google firebase and display in list view in android studio app
My team and I are working on an android studio project. 我和我的团队正在从事一个android studio项目。 We can add data to the firebase.
我们可以将数据添加到Firebase。 We are trying to retrieve data from the database but we think that we cannot reference the correct ID to retrieve data in a list view.
我们正在尝试从数据库中检索数据,但是我们认为我们无法引用正确的ID来在列表视图中检索数据。 We are trying to retrieve the amount, type and start under the Feedings Class.
我们正在尝试检索数量,类型并在Feedings类下开始。 This is how our firebase database is set up.
这就是我们的firebase数据库的设置方式。
Here is my code: 这是我的代码:
@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();
}
} }
try to use the reference like this, it's provided in the official docs. 尝试使用像这样的参考,它在官方文档中提供。 and be aware of your ShowData method it's not correct.
并注意您的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.