Arraylist的对象从Firebase

Arraylist的对象从Firebase

本文介绍了无法转换类型为java.util.Arraylist的对象从Firebase Realtim数据库中读取嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处输入图片描述

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;
}

}

Second class is my nested object I think
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;
}

}

    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();
    }
}

推荐答案

您遇到以下错误:

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

Because you getting an ArrayList object and not a Counters object and there is no way in Java to create such a cast. Your counters node that exists within the cRVh ... NHeT node is actually an array. When you try to read that array in code, it is read as an ArrayList, hence that error. As I see in your screenshot, that array contains Counters objects. Unfortunately, you cannot simply map that array of Counters objects to an ArrayList<Counters>. The counters node is actually an ArrayList of HashMaps. So if you need to have an ArrayList<Counters>, then you should iterate and create that List yourself. Besides that, the name of the properties in your Counters class do not match the names in the database. See, countername vs name and counternumber vs counter. All properties must match.

这篇关于无法转换类型为java.util.Arraylist的对象从Firebase Realtim数据库中读取嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:06