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

huangapple 未分类评论46阅读模式
英文:

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&lt;upload&gt; uploadPdf;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cp);
    
            ListPdf = (ListView) findViewById(R.id.List);
            uploadPdf = new ArrayList&lt;&gt;();
            ViewAllPdf();
        }
    
        private void ViewAllPdf() {
            databaseReference = FirebaseDatabase.getInstance().getReference(&quot;year_1&quot;);
            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&lt;uploads.length;i++){
                        uploads[i] = uploadPdf.get(i).getName();
                    }
                    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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&#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**

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.

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

发表评论

匿名网友

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

确定