我可以在一个活动中创建一个对父节点的引用,在另一个活动中创建另一个对其子节点的引用吗?

时间:2017-09-09 05:31:30

标签: android firebase

当用户点击列表项(节点)时,会打开一个新活动,显示firebase数据库中该列表项的所有子项,但是当我单击列表项时,该应用程序正在关闭。这是我的代码,用于显示所点击用户名的所有子项。请帮帮我,谢谢。

  friend = getIntent().getExtras().get("user_name").toString();


    setTitle(friend+"'s"+" "+"status's");
    Toast.makeText(getApplicationContext(),friend,Toast.LENGTH_SHORT).show();

    uploadList = new ArrayList<>();
    listView = (ListView) findViewById(R.id.statusList);
    player = new MediaPlayer();

    sDatabase = FirebaseDatabase.getInstance().getReference(friend);

    sDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                uploadList.add(upload);
            }
            String[] uploads = new String[uploadList.size()];

            for (int i = 0; i < uploads.length; i++) {
                uploads[i] = uploadList.get(i).getName();
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, uploads);
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getApplicationContext(), (CharSequence) databaseError,Toast.LENGTH_SHORT).show();


        }
    });

1 个答案:

答案 0 :(得分:0)

我已经自己解决了.. 而不是onDataChange

中的这两行代码

上传upload = postSnapshot.getValue(Upload.class);                 uploadList.add(上载);

将其更改为..

uploadList.add(postSnapshot.getValue(Upload.class));