public class FacultiesActivity extends AppCompatActivity {
private ListView staffLlistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initComponent();
setContentView(R.layout.activity_faculties);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("staff");
Query mRef = myRef.child("1");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
Staff staff = dataSnapshot.getValue(Staff.class);
String name =staff.getName();
Toast.makeText(FacultiesActivity.this,name , Toast.LENGTH_LONG).show();
}catch (Exception ex){
// Toast.makeText(FacultiesActivity.this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError error) {
Toast.makeText(FacultiesActivity.this, "Error", Toast.LENGTH_LONG).
});
}
private void initComponent() {
genrateId();
registerEvents();
}
private void genrateId() {
staffLlistView=findViewById(R.id.staffListView);
}
private void registerEvents() {
}
}
staff.java:
package com.example.shant.kdkce.Contrrollers;
public class Staff {
private String department;
private String designation;
private String email;
private String image;
private String name;
private String qualification;
public Staff() {
}
public Staff(String department, String designation, String email, String image, String name, String qualification) {
this.department = department;
this.designation = designation;
this.email = email;
this.image = image;
this.name = name;
this.qualification = qualification;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
}
数据库:
想要从firebase数据库中读取数据并将其显示到列表或卡中,但甚至无法从数据库中获取数据并在toast内部显示需要帮助从数据库中读取数据
在 com.example.shant.kdkce.FacultiesActivity $ 1.onDataChange(FacultiesActivity.java:43) 在com.google.android.gms.internal.firebase_database.zzfc.zza(未知 来源:13) 在com.google.android.gms.internal.firebase_database.zzgx.zzdr(未知 来源:2) 在com.google.android.gms.internal.firebase_database.zzhd.run(未知 来源:71) 在android.os.Handler.handleCallback(Handler.java:789) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:172) 在android.app.ActivityThread.main(ActivityThread.java:6637) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)