gpt4 book ai didi

android - Firebase @PropertyName 不起作用

转载 作者:IT老高 更新时间:2023-10-28 22:23:34 25 4
gpt4 key购买 nike

故事

我在我的应用中使用 Firebase 实时数据库。我有一个类似这样的模型。

class Item {
int mItemName;
// Simplified for brevity
}

现在,这会将字段作为 itemName 存储在我的实时数据库中。但我不想使用该命名约定。我希望命名模式是这样的,item_name

我做了什么

我像这样在字段上方使用了@PropertyName("item_name"),

class Item {
@PropertyName("item_name")
int mItemName;
// Simplified for brevity
}

问题

Firebase 似乎完全忽略了注释。我无法更改序列化和反序列化的属性名称。

任何帮助将不胜感激。

编辑

这是完整的模型类,

public class FileModel {

@PropertyName("file_id")
String mFileId;
@PropertyName("file_name")
String mOriginalFileName;
@PropertyName("file_path")
String mFilePath;
@PropertyName("file_type")
String mFileType;
@PropertyName("last_modified")
Long mFileLastModified;
@PropertyName("file_size")
String mFileSize;
@Exclude
private boolean mIsSelected;

/**
* Must have empty constructor for JSON deserialization by Firebase
*/
public FileModel() {
}

public FileModel(String fileId, String originalFileName,
String filePath, String fileType, Long fileLastModified, String fileSize) {
this.mFileId = fileId;
this.mOriginalFileName = originalFileName;
this.mFilePath = filePath;
this.mFileType = fileType;
this.mFileLastModified = fileLastModified;
this.mFileSize = fileSize;
}

public String getFileId() {
return mFileId;
}

public void setFileId(String fileId) {
this.mFileId = fileId;
}

public String getOriginalFileName() {
return mOriginalFileName;
}

public void setOriginalFileName(String originalFileName) {
this.mOriginalFileName = originalFileName;
}

public String getFilePath() {
return mFilePath;
}

public void setFilePath(String filePath) {
this.mFilePath = filePath;
}

public String getFileType() {
return mFileType;
}

public void setFileType(String fileType) {
this.mFileType = fileType;
}

public Long getFileLastModified() {
return mFileLastModified;
}

public void setFileLastModified(Long fileLastModified) {
this.mFileLastModified = fileLastModified;
}

public String getFileSize() {
return mFileSize;
}

public void setFileSize(String fileSize) {
this.mFileSize = fileSize;
}

public boolean getIsSelected() {
return mIsSelected;
}

public void setIsSelected(boolean isSelected) {
this.mIsSelected = isSelected;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

FileModel model = (FileModel) o;

if (mIsSelected != model.mIsSelected) return false;
if (mFileId != null ? !mFileId.equals(model.mFileId) : model.mFileId != null) return false;
if (mOriginalFileName != null ? !mOriginalFileName.equals(model.mOriginalFileName) : model.mOriginalFileName != null)
return false;
if (mFilePath != null ? !mFilePath.equals(model.mFilePath) : model.mFilePath != null)
return false;
if (mFileType != null ? !mFileType.equals(model.mFileType) : model.mFileType != null)
return false;
if (mFileLastModified != null ? !mFileLastModified.equals(model.mFileLastModified) : model.mFileLastModified != null)
return false;
return mFileSize != null ? mFileSize.equals(model.mFileSize) : model.mFileSize == null;

}

@Override
public int hashCode() {
int result = mFileId != null ? mFileId.hashCode() : 0;
result = 31 * result + (mOriginalFileName != null ? mOriginalFileName.hashCode() : 0);
result = 31 * result + (mFilePath != null ? mFilePath.hashCode() : 0);
result = 31 * result + (mFileType != null ? mFileType.hashCode() : 0);
result = 31 * result + (mFileLastModified != null ? mFileLastModified.hashCode() : 0);
result = 31 * result + (mFileSize != null ? mFileSize.hashCode() : 0);
result = 31 * result + (mIsSelected ? 1 : 0);
return result;
}

@Override
public String toString() {
return "FileModel{" +
"mFileId='" + mFileId + '\'' +
", mOriginalFileName='" + mOriginalFileName + '\'' +
", mFilePath='" + mFilePath + '\'' +
", mFileType='" + mFileType + '\'' +
", mFileLastModified=" + mFileLastModified +
", mFileSize='" + mFileSize + '\'' +
", mIsSelected=" + mIsSelected +
'}';
}
}

最佳答案

Kotlin 数据类的解决方案:

data class Pojo (@get:PropertyName("fieldName") @set:PropertyName("fieldName") var field: String = "")

关于android - Firebase @PropertyName 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com