在数组中,它存储的是NULL,而不是PDF文件名。

huangapple go评论109阅读模式
英文:

In array it is storing NULL instead of PDF file name

问题

  1. public class cp extends AppCompatActivity {
  2. ListView ListPdf;
  3. DatabaseReference databaseReference;
  4. List<upload> uploadPdf;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_cp);
  9. ListPdf = (ListView) findViewById(R.id.List);
  10. uploadPdf = new ArrayList<>();
  11. ViewAllPdf();
  12. }
  13. private void ViewAllPdf() {
  14. databaseReference = FirebaseDatabase.getInstance().getReference("year_1");
  15. databaseReference.addValueEventListener(new ValueEventListener() {
  16. @Override
  17. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  18. for(DataSnapshot PostSnapShot: dataSnapshot.getChildren()){
  19. upload Pdf = PostSnapShot.getValue(upload.class);
  20. uploadPdf.add(Pdf);
  21. }
  22. String[] uploads = new String[uploadPdf.size()];
  23. for(int i=0;i<uploads.length;i++){
  24. uploads[i] = uploadPdf.get(i).getName();
  25. }
  26. ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, uploads);
  27. ListPdf.setAdapter(adapter);
  28. }
  29. @Override
  30. public void onCancelled(@NonNull DatabaseError databaseError) {
  31. }
  32. });
  33. }
  34. }
  1. public class upload {
  2. public String name;
  3. public upload() {
  4. this.name = name;
  5. }
  6. public String getName() {
  7. return name;
  8. }
  9. }

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]

  1. Please let me know if you need any further assistance or clarification.
  2. <details>
  3. <summary>英文:</summary>
  4. public class cp extends AppCompatActivity {
  5. ListView ListPdf;
  6. DatabaseReference databaseReference;
  7. List&lt;upload&gt; uploadPdf;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_cp);
  12. ListPdf = (ListView) findViewById(R.id.List);
  13. uploadPdf = new ArrayList&lt;&gt;();
  14. ViewAllPdf();
  15. }
  16. private void ViewAllPdf() {
  17. databaseReference = FirebaseDatabase.getInstance().getReference(&quot;year_1&quot;);
  18. databaseReference.addValueEventListener(new ValueEventListener() {
  19. @Override
  20. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  21. for(DataSnapshot PostSnapShot: dataSnapshot.getChildren()){
  22. upload Pdf = PostSnapShot.getValue(upload.class);
  23. uploadPdf.add(Pdf);
  24. }
  25. String[] uploads = new String[uploadPdf.size()];
  26. for(int i=0;i&lt;uploads.length;i++){
  27. uploads[i] = uploadPdf.get(i).getName();
  28. }
  29. ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getApplicationContext(),android.R.layout.simple_list_item_1,uploads);
  30. ListPdf.setAdapter(adapter);
  31. }
  32. @Override
  33. public void onCancelled(@NonNull DatabaseError databaseError) {
  34. }
  35. });
  36. }
  37. }
  38. upload class
  39. public class upload {
  40. public String name;
  41. public upload() {
  42. this.name = name;
  43. }
  44. public String getName() {
  45. return name;
  46. }
  47. }
  48. **In uploads[ ] it is storing null instead of file name,i cannot figure out what&#39;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**
  49. Firebase database screenshot
  50. [![enter image description here][1]][1]
  51. [1]: https://i.stack.imgur.com/mwAcI.png
  52. </details>
  53. # 答案1
  54. **得分**: 0
  55. 你代码中的问题出在你的 POJO ```Upload``` 上。Firebase 文档中有一个示例:
  56. ```java
  57. public class User {
  58. public String username;
  59. public String email;
  60. public User() {
  61. // 必须有默认构造函数,以便调用 DataSnapshot.getValue(User.class)
  62. }
  63. public User(String username, String email) {
  64. this.username = username;
  65. this.email = email;
  66. }
  67. }

此外,你的实时数据库中的字段名称与你的 POJO 类不匹配。

英文:

The problem with your code is your POJO class Upload. See an example from Firebase Documentation

  1. public class User {
  2. public String username;
  3. public String email;
  4. public User() {
  5. // Default constructor required for calls to DataSnapshot.getValue(User.class)
  6. }
  7. public User(String username, String email) {
  8. this.username = username;
  9. this.email = email;
  10. }
  11. }

Also the fields name in your Realtime Database doesn't match with your POJO class.

huangapple
  • 本文由 发表于 2020年5月5日 12:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61605750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定