我正在使用Google Firebase和FirebaseUI
我将值列表传递给FirebaseListAdapter,如此
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
requestList request = requestSnapshot.getValue(requestList.class);
Log.i("Chat", request.getTitle()+": "+request.getDescription()+" "+request.getKey());
}
我想存储记录索引(Key)以及其中的信息。但是,如果字段自动映射,如果它们是相同的名称,这将使我无法解决它。
我试过调用"键"的属性。 requestList类中的方法名称为" key"然而,这没有用
返回的数据结构如下所示
Key:{
"RequestTitle":"Value",
"RequestDescription":"description",
"RequestItems":{ item 1 item 2}
}
请求类看起来像这样
public class requestList {
String RequestTitle;
String RequestDescription;
String key;
public requestList() {
}
public requestList(String RequestTitle, String RequestDescription, String key) {
this.RequestTitle = RequestTitle;
this.RequestDescription = RequestDescription;
this.key = key;
}
public String getTitle() {
return RequestTitle;
}
public String getKey() {
return key;
}
public String getDescription() {
return RequestDescription;
}
}
答案 0 :(得分:1)
当您致电if document.submissions.create(submission_params) && document.update_attributes(creator_id: current_user.id)
render json: document, status: 200
else
render status: 400
end
时,您正在从数据库中获取JSON 值并将其转换为requestSnapshot.getValue(requestList.class)
对象。 键不属于此类。
但是,您可以轻松调用requestList
来获取每个快照的密钥。
如果你想保留密钥和值,可能只需保留requestSnapshot.getKey()
,因为它包含两者。
答案 1 :(得分:1)
我赞成上面的答案,因为我不能用输入,但这是工作的答案
我将requestList类更改为包含setKey方法,然后在onDataChange方法中调用它。不确定它是否是正确的方法,但它确实解决了问题
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
//requestList request = requestSnapshot.getValue(requestList.class);
requestList request = requestSnapshot.getValue(requestList.class);
request.setKey(requestSnapshot.getKey());
Log.i("Chat", request.getTitle()+": "+request.getDescription()+" "+request.getKey());
}
}
和requestList类
public class requestList {
String RequestTitle;
String RequestDescription;
String key;
DataSnapshot requestSnapshot;
public requestList() {
}
public requestList(DataSnapshot requestSnapshot) {
//this.RequestTitle = RequestTitle;
//this.RequestDescription = RequestDescription;
//this.key = key;
this.requestSnapshot = requestSnapshot;
}
public String getTitle() {
return RequestTitle;
}
public String getKey() {
return key;
}
public String getDescription() {
return RequestDescription;
}
public void setKey(String key) {
this.key = key;
}
}