在JSON DataSnapshot中获取子级

时间:2019-05-09 01:43:21

标签: java android json firebase firebase-realtime-database

我试图分别从JSON中获取子项,并将它们通过意图传递。这是我的JSON格式:

  "allDeeJays" : {
    "-LeP1DB6Onzh4-UiN_0E" : {
      "acct" : "Aaron A",
      "djName" : "uhgvvvbbb"
    }
  },

使用DataSnapshot,我已经能够使用以下代码获得djName值,但是我没有获得acct值:

 @Override
                    protected void onBindViewHolder(@NonNull ResultsViewHolder holder, final int position, @NonNull final DataSnapshot snapshot) {
                        // Here you convert the DataSnapshot to whatever data your ViewHolder needs
                        String s = "";
                        for(DataSnapshot ds : snapshot.getChildren())
                        {
                            s = ds.getValue(String.class);
                            DjProfile model = new DjProfile(s);
                            model.setDjName(s);

                            holder.setDjProfile(model);
                        }

                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view)
                            {

                                DatabaseReference ref = FirebaseDatabase.getInstance().getReference("allDeeJays");

                                String acct = "";
                                String name = "";

                                for(DataSnapshot ds : snapshot.getChildren())
                                {
                                    name = ds.getValue(String.class);
                                    acct = ds.getValue(String.class);
                                    DjProfile model = new DjProfile(name);
                                    model.setDjName(name);
                                }
                                Intent i = new Intent(getApplication(), AddSongRequest.class);
                                i.putExtra("DjName", name);
                                i.putExtra("UserAcct", acct);
                                startActivity(i);
                            }
                        });
                    }
                };

最后,我的DjProfile类定义如下: 包com.example.android.heydj;

导入com.google.firebase.database.Exclude; 导入com.google.firebase.database.PropertyName;

public class DjProfile
{
    String djName;
    String key;


    public DjProfile(String djName)
    {
        this.djName = djName;
    }
    public DjProfile(){}

    public void setDjName(String djName)
    {
        this.djName = djName;
    }

    public String getdjName()
    {
        return djName;
    }
}

两个变量都返回相同的值,当我运行snapshot.getChildrenCount()时,它说有两个子代(我认为是acct和djName)。我是否需要为帐户名添加其他获取器和设置器?任何帮助,我们将不胜感激:)

2 个答案:

答案 0 :(得分:1)

尝试这样,它将为您的密钥返回准确的值

if(snapShot.getKey().equalsIgnoreCase("djName"))
name = ds.getValue(String.class);

if(snapShot.getKey().equalsIgnoreCase("acct"))
acct = ds.getValue(String.class);

或使用

  DjProfile model = snapShot.getValue(DjProfile.class);

代替

 for(DataSnapshot ds : snapshot.getChildren())
                            {
                                name = ds.getValue(String.class);
                                acct = ds.getValue(String.class);
                                DjProfile model = new DjProfile(name);
                                model.setDjName(name);
                            }

答案 1 :(得分:0)

您可以通过在下面的行-

中添加来解决此问题
DjProfile profile = ds.getValue(DjProfile.class);

您现在可以将此个人资料传递给Intent。