Firebase错误:期望一个列表,但得到一个类java.util.HashMap



我有一个名为Garage的对象,一旦我第一次声明他,它有4个参数:garageName,garageId,garageCoordinatorPhone,garageProducts。我把它上传到firebase。过了一段时间,我更新了firebase中的车库参数:garageStocks。当我尝试将数据返回到Garage类时,我得到了异常:

com.google.firebase.database。DatabaseException:期望在反序列化时得到一个列表,但得到一个类java.util.HashMap

我看到了一个几乎完全一样的问题:期望在反序列化时得到一个列表,但得到一个类java.util.HashMap

但是它的答案对我来说没有意义…我已经能够获得的值,甚至与对象列表(见garageProducts)。

GaragesActivity:

public class GaragesActivity extends AppCompatActivity {
ArrayList<String> garages = new ArrayList<>();
DatabaseReference dbReference = FirebaseDatabase.getInstance().getReference();
RecyclerView rvGarages;
Button tvGarageEdit;
TextView tvGarageCoordinator;
User selectedGarageCoordinator = null;
boolean isEditable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_garages);

//defines the widgets
rvGarages = findViewById(R.id.rvGarages);
tvGarageEdit = findViewById(R.id.tvGarageEdit);
refreshAllGarages();
//defines and sets the toolbar in place.
Toolbar toolbar = findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
//defines the back button at the top corner
ActionBar ab = getSupportActionBar();
if(ab != null)
ab.setDisplayHomeAsUpEnabled(true);

tvGarageEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//the opposite every press
if (isEditable){
isEditable = false;
tvGarageEdit.setBackgroundColor(getResources().getColor(R.color.colorWritingImportant));
}else{
isEditable = true;
tvGarageEdit.setBackgroundColor(getResources().getColor(R.color.colorLimeGreen));
}
}
});
}

private void refreshAllGarages() {
//reset
garages.clear();
GaragesAdapter myAdapter = new GaragesAdapter(GaragesActivity.this, garages);
rvGarages.setAdapter(myAdapter);
final Dialog myDialog = new Dialog(GaragesActivity.this);
myDialog.setContentView(R.layout.dialog_progress);
if(myDialog.getWindow() != null)
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
TextView tvAction = myDialog.findViewById(R.id.tvAction);
tvAction.setText("טוען...");
myDialog.show();
// Read from the database
Query q = dbReference.child("מחסנים").orderByKey();
q.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot AllGarages : dataSnapshot.getChildren()) //gets the list of Garages
{
Garage g = AllGarages.getValue(Garage.class);
if(g.getGarageName() != null)
garages.add(g.getGarageName());
}
GaragesAdapter myAdapter = new GaragesAdapter(GaragesActivity.this, garages);
rvGarages.setLayoutManager(new GridLayoutManager(GaragesActivity.this,4));
myDialog.dismiss();
rvGarages.setAdapter(myAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); //Don't ignore errors
}
});
}
}

车库类:

public class Garage {
private String garageName;
private String garageId;
private String garageCoordinatorPhone;
private ArrayList<Product> garageProducts;
private ArrayList<Product> borrowedProducts;
private ArrayList<Product> damagedProducts;
private ArrayList<Product> missingProducts;
private ArrayList<Stock> garageStocks;
public Garage(){}
public Garage(String garageName, String garageCoordinatorPhone, String garageId, ArrayList<Product> garageProducts){
this.garageProducts = garageProducts;
this.garageName = garageName;
this.garageCoordinatorPhone = garageCoordinatorPhone;
this.garageId = garageId;
}
public Garage(String garageName, String garageCoordinatorPhone, String garageId, ArrayList<Product> garageProducts, ArrayList<Stock> garageStocks){
this.garageProducts = garageProducts;
this.garageStocks = garageStocks;
this.garageName = garageName;
this.garageCoordinatorPhone = garageCoordinatorPhone;
this.garageId = garageId;
}
public Garage(String garageName, String garageCoordinatorPhone, String garageId, ArrayList<Product> garageProducts, ArrayList<Stock> garageStocks, ArrayList<Product> borrowedProducts){
this.garageProducts = garageProducts;
this.garageStocks = garageStocks;
this.garageName = garageName;
this.garageCoordinatorPhone = garageCoordinatorPhone;
this.garageId = garageId;
this.borrowedProducts = borrowedProducts;
}
public Garage(String garageName, String garageCoordinatorPhone, String garageId, ArrayList<Product> garageProducts, ArrayList<Stock> garageStocks, ArrayList<Product> borrowedProducts, ArrayList<Product> damagedProducts, ArrayList<Product> missingProducts){
this.garageProducts = garageProducts;
this.garageStocks = garageStocks;
this.garageName = garageName;
this.garageCoordinatorPhone = garageCoordinatorPhone;
this.garageId = garageId;
this.borrowedProducts = borrowedProducts;
this.damagedProducts = damagedProducts;
this.missingProducts = missingProducts;
}
}

重火力点:

{
"173baaa74c0" : {
"garageCoordinatorPhone" : "0523122548",
"garageId" : "173baaa74c0",
"garageName" : "רעננה מרכז",
"garageProducts" : [ {
"description" : "אורך 1.7 מטר",
"productAmount" : 5,
"productCategory" : "בנייה מחנאית",
"productId" : 201,
"productName" : "בזנט"
}, {
"description" : "נפח 500 סמ'ק",
"productAmount" : 5,
"productCategory" : "כיבוי אש",
"productId" : 402,
"productName" : "מטפים תקינים"
}, {
"description" : "אורך 30 ס'מ",
"productAmount" : 4,
"productCategory" : "כלי עבודה",
"productId" : 506,
"productName" : "שפכטל"
}, {
"description" : "אורך 30 ס'מ",
"productAmount" : 1,
"productCategory" : "חשמל",
"productId" : 323,
"productName" : "DI"
}, {
"description" : "סיר מתכת לטיולים, קוטר 15 ס'מ",
"productAmount" : 7,
"productCategory" : "בישול",
"productId" : 127,
"productName" : "סיר בינוני"
}, {
"description" : "",
"productAmount" : 1,
"productCategory" : "חשמל",
"productId" : 309,
"productName" : "מיקרופון"
} ],
"garageStocks" : {
"1610831472776" : {
"stockFillerPhone" : "0544926410",
"stockId" : 1610831472776,
"stockProducts" : [ {
"description" : "אורך 1.7 מטר",
"productAmount" : 5,
"productCategory" : "בנייה מחנאית",
"productId" : 201,
"productName" : "בזנט"
}, {
"description" : "נפח 500 סמ'ק",
"productAmount" : 5,
"productCategory" : "כיבוי אש",
"productId" : 402,
"productName" : "מטפים תקינים"
}, {
"description" : "אורך 30 ס'מ",
"productAmount" : 4,
"productCategory" : "כלי עבודה",
"productId" : 506,
"productName" : "שפכטל"
}, {
"description" : "אורך 30 ס'מ",
"productAmount" : 1,
"productCategory" : "חשמל",
"productId" : 323,
"productName" : "DI"
}, {
"description" : "סיר מתכת לטיולים, קוטר 15 ס'מ",
"productAmount" : 7,
"productCategory" : "בישול",
"productId" : 127,
"productName" : "סיר בינוני"
}, {
"description" : "",
"productAmount" : 1,
"productCategory" : "חשמל",
"productId" : 309,
"productName" : "מיקרופון"
} ]
}
}
},
"1740c8d4920" : {
"garageCoordinatorPhone" : "0544926411",
"garageId" : "1740c8d4920",
"garageName" : "אשדוד ט'ו",
"garageProducts" : [ {
"description" : "",
"productAmount" : 2,
"productCategory" : "שונות",
"productHome" : "173baaa74c0",
"productId" : 1102,
"productName" : "דרבוקות גדולות"
}, {
"description" : "נפח 500 סמ'ק",
"productAmount" : 2,
"productCategory" : "כיבוי אש",
"productHome" : "173baaa74c0",
"productId" : 402,
"productName" : "מטפים תקינים"
} ]
},
"174534895e0" : {
"borrowedProducts" : {
"5f66189e 207aeaca" : [ {
"description" : "סיר מתכת לטיולים, קוטר 15 ס'מ",
"productAmount" : 1,
"productCategory" : "בישול",
"productHome" : "173baaa74c0",
"productId" : 127,
"productName" : "סיר בינוני"
} ]
},
"garageCoordinatorPhone" : "0544926410",
"garageId" : "174534895e0",
"garageName" : "הכפתור",
"garageProducts" : [ {
"description" : "אורך 1.7 מטר",
"productAmount" : 23,
"productCategory" : "בנייה מחנאית",
"productHome" : "173baaa74c0",
"productId" : 201,
"productName" : "בזנט"
}, {
"description" : "",
"productAmount" : 2,
"productCategory" : "שונות",
"productHome" : "173baaa74c0",
"productId" : 1102,
"productName" : "דרבוקות גדולות"
}, {
"description" : "נפח 500 סמ'ק",
"productAmount" : 1,
"productCategory" : "כיבוי אש",
"productHome" : "173baaa74c0",
"productId" : 402,
"productName" : "מטפים תקינים"
} ]
}
}
enter code here

基于您的Firebase JSON,看起来您正在存储一个对象,其中包含garageStocks中每个Stock的字段,而不是像Garage类期望的列表。

例如:

"garageStocks" : {
"stockObject1": { ... },
"stockObject2": { ... },
...
}

然而,你的Garage类需要一个列表:private ArrayList<Stock> garageStocks;,它应该在JSON中如下:

"garageStocks": [
{ stock object 1 },
{ stock object 2 },
...
]

这就是为什么你得到错误,它期望一个列表,而得到一个映射。

要解决这个问题,您有两个选项:

  1. 更改向Firebase推送数据的方式,以确保您正在推送列表
  2. garageStocks的类型更改为HashMap

我不知道你目前正在为选项1做什么,但你似乎正在为Garage类中的产品列表(ArrayList<Product>)做正确的事情,所以这可能是一个看的好地方。

希望这对你有帮助!

即使将hashmap更改为list,问题仍然存在。解释:如果列表中的某项被删除,实时数据库会将该列表重新考虑为哈希映射,然后抛出错误。所以我认为这是firebase的问题,需要他们的团队来修复!

相关内容

最新更新