无法将java.util.ArrayList强制转换为java.util.HashMap Firebase Android [英] java.util.ArrayList cannot be cast to java.util.HashMap Firebase Android
问题描述
我的数据库结构:
a,b,s是我的建筑物名称;1,1,2是楼层号;1,1,3是固定数字,而ra111,rb111,rs231是我的用户ID.我正在尝试实现一个显示建筑物名称的列表视图,然后单击任何建筑物名称,列表视图显示相应的楼层号,然后单击楼层号,它应显示相应的公寓号.
a,b,s are my building names; 1,1,2 are floor numbers; 1,1,3 are flat numbers and ra111,rb111,rs231 are my user ids.
I am trying to implement a list view showing building names and then clicking on any building name the list view shows corresponding floor numbers and clicking on floor number it should show corresponding flat numbers.
DatabaseReference ref,ref2,ref3;
ListView listBuildings,listFloors,listFlats;
ArrayAdapter<String>adapterBuildings,adapterFloors,adapterFlats;
String[] arrayOfBuildings,arrayOfFloors,arrayOfFlats;
ref = FirebaseDatabase.getInstance().getReference("structure");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
HashMap<String,Object>structure = (HashMap<String, Object>) dataSnapshot.getValue();
Set<String>buildings = structure.keySet();
arrayOfBuildings = buildings.toArray(new String[0]);
adapterBuildings = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,arrayOfBuildings);
listBuildings.setAdapter(adapterBuildings);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
listBuildings.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textViewList.setText("Select the floor number");
String building = arrayOfBuildings[position];
Log.i("building",building);
ref2 = FirebaseDatabase.getInstance().getReference("structure").child(building);
listBuildings.setVisibility(View.INVISIBLE);
listFloors.setVisibility(View.VISIBLE);
ref2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.i("datasnapshot",dataSnapshot.getValue().toString());
Log.i("key",dataSnapshot.getKey());
HashMap<String,Object>structure = (HashMap<String, Object>) dataSnapshot.getValue();
Set<String>floors = structure.keySet();
arrayOfFloors = floors.toArray(new String[0]);
adapterFloors = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,arrayOfFloors);
listFloors.setAdapter(adapterFloors);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
listFloors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textViewList.setText("Select the flat number and press submit");
String floor = arrayOfFloors[position];
Log.i("floor",floor);
ref3 = ref2.child(floor);
listFlats.setVisibility(View.VISIBLE);
listFloors.setVisibility(View.INVISIBLE);
ref3.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.i("datasnapshot",dataSnapshot.getValue().toString());
HashMap<String,Object>f = (HashMap<String, Object>) dataSnapshot.getValue();
Set<String>flats = f.keySet();
arrayOfFlats = flats.toArray(new String[0]);
adapterFlats = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,arrayOfFlats);
listFlats.setAdapter(adapterFlats);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
但是当我尝试以明智的方式检索数据时:它显示了我
一个
b
s
单击s时,它向我显示正确的楼层:
2
但是,如果我单击建筑物a或建筑物b:
它给了我错误:无法将java.util.ArrayList强制转换为java.util.HashMap.如果我记录datasnapShot值,则会显示:[null,[null,{rb111 = default}]].我不明白为什么会这样,为什么它能正确地构建's'.
请帮助!
感谢您的时间:)
But when I try to retrieve data building wise:
it shows me
a
b
s
On clicking on s, it shows me correct floor:
2
But if I click on building a or building b:
It gives me error: java.util.ArrayList cannot be cast to java.util.HashMap. If I log the datasnapShot values, it shows me: [null, [null, {rb111=default}]].
I am not understanding why this is happening and why it works correctly for building 's'.
Please help!
Thanks for your time :)
推荐答案
它告诉我:[null,[null,{rb111 = default}]].我不明白为什么会这样
it shows me: [null, [null, {rb111=default}]]. I am not understanding why this is happening
显示 null
的原因,这是您的Firebase数据树的一些子代.如果您将数字用作Firebase数据库中的键,则将其视为数组.
Reason to show null
here is your firebase data tree`s some children are numbers. If you use numbers as keys in firebase db it consider as a array.
如果仅使用 1
作为kay名称,则表示它是数组的第二个元素.Firebase认为数组的第一个元素未定义.所以这是空的.在 [null,[null,{rb111 = default}]].
中,未定义 0
元素,但已定义的 1
元素为[null,{rb111 = default}]
.在此 0
元素中未定义,但定义的 1
是 {rb111 = default}
If you only use 1
as kay name that means it is second element of a array. Firebase thinks first element of array is undefined. So that is null. In [null, [null, {rb111=default}]].
, 0
element is undefined but 1
is defined that is [null, {rb111=default}]
. In this 0
element is undefined but 1
is defined that is {rb111=default}
我想您现在已经理解了为什么数据对象中存在空对象
I think now you understood why null object in your data object
这篇关于无法将java.util.ArrayList强制转换为java.util.HashMap Firebase Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!