英文:
In array it is storing NULL instead of PDF file name
问题
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;
}
}
In uploads[ ] it is storing null instead of file name, i cannot figure out what's the problem. I tried logcat for debugging but it shows Pdf object has three files but in uploads[ ] it stores NULL. Please look into it
Firebase database screenshot:
[![enter image description here][1]][1]
Please let me know if you need any further assistance or clarification.
<details>
<summary>英文:</summary>
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) {
}
});
}
}
upload class
public class upload {
public String name;
public upload() {
this.name = name;
}
public String getName() {
return name;
}
}
**In uploads[ ] it is storing null instead of file name,i cannot figure out what's the problem. I tried logcat for debuging but it shows Pdf object has three files but in uploads[ ] it stores NULL. Please look into it**
Firebase database screenshot
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/mwAcI.png
</details>
# 答案1
**得分**: 0
你代码中的问题出在你的 POJO 类 ```Upload``` 上。Firebase 文档中有一个示例:
```java
public class User {
public String username;
public String email;
public User() {
// 必须有默认构造函数,以便调用 DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
此外,你的实时数据库中的字段名称与你的 POJO 类不匹配。
英文:
The problem with your code is your POJO class Upload
. See an example from Firebase Documentation
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;
}
}
Also the fields name in your Realtime Database doesn't match with your POJO class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论