【发布时间】: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