如何在Android Firebase数据库中为变量设置不同的名称?
我有一门课叫“某物”。我需要将这个类保存到Firebase,不是作为“某物”,而是作为“某物”。我可以用HashMap保存它,但我不清楚如何返回值(主要是针对适配器) 将数据转换为哈希映射以进行保存:如何在Android Firebase数据库中为变量设置不同的名称?,android,hashmap,firebase-realtime-database,Android,Hashmap,Firebase Realtime Database,我有一门课叫“某物”。我需要将这个类保存到Firebase,不是作为“某物”,而是作为“某物”。我可以用HashMap保存它,但我不清楚如何返回值(主要是针对适配器) 将数据转换为哈希映射以进行保存: public Map<String, Object> toMap() { HashMap<String, Object> result = new HashMap<>(); result.put("some_thing", something_v
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("some_thing", something_value);
return result;
}
publicmap-toMap(){
HashMap结果=新建HashMap();
结果。放置(“某物”,某物的价值);
返回结果;
}
但如何将它们加载回适配器?适配器是基本适配器:
public class SomethingAdapter extends ArrayAdapter<Something> {
public SomethingAdapter(Context context, int resource) {
super(context, resource);
}
}
公共类SomethingAdapter扩展了ArrayAdapter{
公共SomethingAdapter(上下文,int资源){
超级(上下文、资源);
}
}
您可以使用Firebase批注您可以使用Firebase批注感谢,以下是解决方案:
public class Something {
@PropertyName("some_thing")
private int someThing;
public Something(int someThing) {
this.someThing = someThing;
}
@PropertyName("some_thing")
public int getSomeThing() {
return someThing;
}
@PropertyName("some_thing")
public void setSomeThing(int someThing) {
this.someThing = someThing;
}
}
感谢,以下是解决方案:
public class Something {
@PropertyName("some_thing")
private int someThing;
public Something(int someThing) {
this.someThing = someThing;
}
@PropertyName("some_thing")
public int getSomeThing() {
return someThing;
}
@PropertyName("some_thing")
public void setSomeThing(int someThing) {
this.someThing = someThing;
}
}