错误-com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.project_sewing.Event1类型

时间:2020-01-27 18:10:44

标签: java android firebase android-studio compiler-errors

我收到此错误-粗体标题上的“ com.google.firebase.database.DatabaseException:无法将类型java.lang.String的对象转换为类型com.example.project_sewing.Event1”。 我创建了一个用于队列管理的应用,我有一个字符串映射(电话号码)和所有事件的ArrayList。

        myRef.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                 for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                        String key = childSnapshot.getKey();
                        if(key.equals("clients")) {
                            allEvents = childSnapshot.getValue(AllEvents.class);
                        } else if(key.equals("manager")) {
                            manager = childSnapshot.getValue(Manager.class);
                        }
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
            }
        });

这是事件1:

public class Event1 {
    private String number,type,messages,hour;
    private int price,day,year,month,status;

    public Event1(String number, String type, int price, int day, int month, int year, String hour,int status, String messages) {}


    public String getNumber() {
        return number;
    }

    public Event1(){}

    public void setMessages(Event1 event) {
        this.messages = "The event:" + event+ " is cancel";
    }

    public int calculationCost(){ }

      @Override
    public String toString() {}
}

这是AllEvents:

public class AllEvents {
    private Map<String, ArrayList<Event1>> data;

    public Map<String,ArrayList<Event1>> getData() {
        return data;
    }

    public AllEvents() {
         data = new HashMap<String, ArrayList<Event1>>();
    }

    public void setData(Map<String,ArrayList<Event1>> data) {

        this.data = data;
    }
    public void addToList(String mapKey,Event1 event){
        ArrayList<Event1> itemsList = data.get(mapKey);
        if(itemsList == null) {
            itemsList = new ArrayList<Event1>();
            itemsList.add(event);
            data.put(mapKey, itemsList);
        } else
            if(!itemsList.contains(event)) itemsList.add(event);
    }
    public AllEvents(Map<String,ArrayList<Event1>> map) {
        if(this.data == null)
            data = new HashMap<String,ArrayList<Event1>>();
        else
            this.data = map;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public boolean isExist(Event1 e)
    {
        if(!data.containsKey(e.getNumber()))
            return false;
        ArrayList<Event1> elist =  data.get(e.getNumber());
        for (Event1 eitem: elist) {
            if(eitem.equals(e))
                return true;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的数据库结构,您将从children firebase 获得多个query。您必须像下面这样处理:

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

    for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {

        String key = childSnapshot.getKey();

        if(key.equals("clients")) {
            allEvents = childSnapshot.getValue(AllEvents.class);
        } else if(key.equals("manager")) {
            manager = childSnapshot.getValue(Manager.class);
        }
    }
}

也如下更改您的Event1

public class Event1 {
    private String number,type,messages,hour;
    private int price,day,year,month,status;

    public Event1(String number, String type, int price, int day, int month, int year, String hour,int status, String messages) {}


    public String getNumber() {
        return number;
    }

    public Event1(){}

    public int calculationCost(){ }

    @Override
    public String toString() {}
}