【问题标题】:Get Children in JSON DataSnapshot在 JSON DataSnapshot 中获取子项
【发布时间】:2019-09-26 19:08:46
【问题描述】:

我正在尝试分别从我的 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)。我是否需要为帐户名称添加额外的 getter 和 setter?非常感谢任何帮助:)

【问题讨论】:

  • 鉴于您为name 和acct 分配了相同的ds.getValue(String.class); 值,因此这些变量具有相同的值是有意义的。除此之外,如果不查看侦听器的代码、将其附加到的位置以及可以重现问题的最小 DjProfile 类,就不可能说出发生了什么。
  • 感谢@FrankvanPuffelen 更新了问题

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


【解决方案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);
                            }

【讨论】:

    【解决方案2】:

    您可以通过添加以下行来解决此问题 -

    DjProfile profile = ds.getValue(DjProfile.class);
    

    您现在可以将此配置文件传递给 Intent。

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2021-12-20
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多