小编典典

无法转换类型为java.util.Arraylist的对象从Firebase Realtim数据库中读取嵌套对象

java

此Firebase数据库结构:

在此处输入图片说明

这是我的设备类:

public class Device {

private String title;
private Map<String,Counters> CounterNumber;
private Counters counters;

public void setCounter(Map<String,Counters> CounterNumber){
    this.CounterNumber=CounterNumber;
}
public Map<String,Counters> getCounter(){
    return CounterNumber;
}
public Counters getCounters(){
    return counters;
}
public void setCounters(Counters counters){
    this.counters=counters;
}
public Device(){

}
public Device(String title){
    this.title=title;
}

public String getTitle(){
    return title;
}
public void setTitle(String title){
    this.title=title;
}

}

第二类是我认为的嵌套对象

第二类Counters Class我的问题是我不知道如何Counter使用FirebaseUI和从数据库中读取对象RecycleView

public class Counters implements Serializable {
private String countername;
private String counternumber;
public Counters (){

}
public Counters(String counternumber,String countername){
    this.counternumber=counternumber;
    this.countername=countername;
}
public String getCounterName() {
    return countername;
}

public void setCounterName(String counterName) {
    this.countername = countername;
}

public String getCounterNumber() {
    return counternumber;
}

public void setCounterNumber(String counternumber) {
    this.counternumber = countername;
}

}

这是我的HomeActivity从Firebase对象读取的内容

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    firebaseDatabase=FirebaseDatabase.getInstance();
    databaseReference=firebaseDatabase.getReference();
    mDevice=new ArrayList<Device>();
    mdevices=new Device();

    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            if (dataSnapshot != null && dataSnapshot.getValue() != null) {
                try {
                    for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
                        ArrayList<String> counterse= (ArrayList<String>) eventSnapshot.getValue();
                        Log.e(TAGE, counterse.toString());
                        Device device = eventSnapshot.getValue(Device.class);
                        String id = eventSnapshot.getKey();
                        Counters counters = eventSnapshot.child(id).getValue(Counters.class);
                        device.setCounters(counters);
                        mDevice.add(device);
                    }

                } catch (Exception ex) {
                    Log.e(TAG, ex.getMessage());
                }

            }
            displayDevices();
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    return view;
}
private void displayDevices(){
    options=
            new FirebaseRecyclerOptions.Builder<Device>()
                    .setQuery(databaseReference.child("devices").orderByChild("user_id").equalTo(userid),Device.class).build();


    adapter=
            new FirebaseRecyclerAdapter<Device, MyRecyclerViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, int position, @NonNull Device model) {

                    mdevices=mDevice.get(position);
                    holder.txt_title.setText(model.getTitle());
                    holder.txt_counter.setText(mdevices.getCounters().getCounterNumber());
                    Log.i(TAG,mdevices.getCounters().getCounterNumber());

                }

                @NonNull
                @Override
                public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View itemView= LayoutInflater.from(getContext()).inflate(R.layout.device_list,parent,false);
                    return new MyRecyclerViewHolder(itemView);
                }
            };

    adapter.startListening();
    recyclerView.setAdapter(adapter);

}
@Override
public void onStart() {
   adapter.startListening();
    super.onStart();
}

@Override
public void onStop() {
    super.onStop();
    if(adapter != null) {
        adapter.stopListening();
    }
}

这是我的错误代码,请 在此处输入图片描述


阅读 287

收藏
2020-11-26

共1个答案

小编典典

您收到以下错误:

无法将类型java.util.ArrayList的对象转换为com.example.smartappfirebase.Counters类型

因为你得到一个ArrayList对象, 不是
一个Counters对象,并没有在Java中没有办法创造这样一个演员。你counters的内存在节点cRVh ... NHeT节点实际上是一个数组。当您尝试在代码中读取该数组时,它将作为ArrayList读取,因此会出现该错误。正如您在屏幕截图中看到的那样,该数组包含Counters对象。不幸的是,您不能简单地将该Counters对象数组映射到ArrayList<Counters>。该counters节点实际上是HashMaps的ArrayList。因此,如果您需要一个ArrayList<Counters>,则应该迭代并自己创建该列表。除此之外,Counters类中属性的名称与数据库中的名称
匹配。看,counternamevs namecounternumbervscounter。所有属性必须匹配。

2020-11-26