public class cp extends AppCompatActivity {
ListView ListPdf;
DatabaseReference databaseReference;
List<upload> uploadPdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cp);
ListPdf = (ListView) findViewById(R.id.List);
uploadPdf = new ArrayList<>();
ViewAllPdf();
}
private void ViewAllPdf() {
databaseReference = FirebaseDatabase.getInstance().getReference("year_1");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot PostSnapShot: dataSnapshot.getChildren()){
upload Pdf = PostSnapShot.getValue(upload.class);
uploadPdf.add(Pdf);
}
String[] uploads = new String[uploadPdf.size()];
for(int i=0;i<uploads.length;i++){
uploads[i] = uploadPdf.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,uploads);
ListPdf.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
上课
public class upload {
public String name;
public upload() {
this.name = name;
}
public String getName() {
return name;
}
}
在uploads[]中,它存储的是null而不是文件名,我不知道问题出在哪里。我试着用logcat调试,但显示Pdf对象有三个文件,但在uploads[]中却存储了null。请您研究一下
Firebase数据库截图
你的代码的问题在于你的POJO类。Upload
. 请看Firebase文档中的一个例子
public class User {
public String username;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
另外,你的实时数据库中的字段名与你的POJO类不匹配。