有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用多个节点从firebase检索数据?

我无法解决这个问题:当我尝试从firebase检索数据时,它说在类[class name]上没有[Node name]的setter/字段

我的类是“Ricetta”,每个节点都以Ricetta的名字命名。 Firebase Structure

利塞塔。阶级

public class Ricetta {

    private String nome;
    private String descrizione;
    private int difficolta;
    private double minuti;
    private String passaggi;
    private String tipologia;
    private String autore;
    private List<Ricetta> ricette;

    public Ricetta(){}

    public List<Ricetta> getRicette() {
        return ricette;
    }

    public void setRicette(List<Ricetta> ricette) {
        this.ricette = ricette;
    }

    public Ricetta(String nome, String descrizione, String tipologia) {
        this.nome = nome;
        this.descrizione = descrizione;
        this.tipologia = tipologia;
    }

    public Ricetta(String nome, String descrizione, String tipologia, String autore, int difficolta, double minuti) {
        this.nome = nome;
        this.descrizione = descrizione;
        this.tipologia = tipologia;
        this.autore = autore;
        this.minuti = minuti;
        this.difficolta = difficolta;
    }


    public String getAutore() {
        return autore;
    }

    public void setAutore(String autore) {
        this.autore = autore;
    }

    public void setDifficolta(int difficolta) {
        this.difficolta = difficolta;
    }

    public void setMinuti(double minuti) {
        this.minuti = minuti;
    }

    public void setTipologia(String passaggi) {
        this.passaggi = passaggi;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public void setPassaggi(String passaggi) {
        this.passaggi = passaggi;
    }

    public String getNome() {
        return nome;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public int getDifficolta() {
        return difficolta;
    }

    public double getMinuti() {
        return minuti;
    }

    public String getPassaggi() {
        return passaggi;
    }

    public String getTipologia() {
        return tipologia;
    }

}

我的HomeFragment的OnCreateView方法。爪哇

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final FirebaseDatabase database = FirebaseDatabase.getInstance("https://cookbook-e6f41-default-rtdb.europe-west1.firebasedatabase.app/");
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference ref = database.getReference().child("users");
    ArrayList<Ricetta> ricette = new ArrayList<>();
    ArrayList<User> utenti = new ArrayList<>();
    View v = inflater.inflate(R.layout.fragment_home, container, false);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());

    ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
           Ricetta r = snapshot.child("ricette").getValue(Ricetta.class);
           System.out.println(r.getAutore());
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

        }

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

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });


    RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this.getContext(), ricette);
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    return v;
}

当我运行时,会显示:

No setter/field for oooooooooo found on class com.cookbook.Ricetta
No setter/field for qweqw found on class com.cookbook.Ricetta
No setter/field for dfsdfs found on class com.cookbook.Ricetta
No setter/field for 9999 found on class com.cookbook.Ricetta
No setter/field for dfsdfdsf found on class com.cookbook.Ricetta
No setter/field for uuuuuuuuu found on class com.cookbook.Ricetta

共 (2) 个答案

  1. # 1 楼答案

    reference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
           for (DataSnapshot s: snapshot.child("ricette").getChildren()) {
               Ricetta r = s.getValue(Ricetta.class);
    Log.d("data",r.getAutore());
            
           }
        }
     
    
  2. # 2 楼答案

    您正在/users上附加一个ChildEventListener。这意味着您的onChildAdded被用户的直接子节点调用,在屏幕截图中是m50...。然后读取那里的ricette子节点的值,这将导致那里的所有9999dfsdfdf等节点

    我猜您正在尝试读取ricette下的一个或所有子节点,为此,您需要使用以下命令在快照的子节点上循环:

    ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
           for (DataSnapshot childSnapshot: snapshot.child("ricette").getChildren()) {
               Ricetta r = childSnapshot.getValue(Ricetta.class);
               System.out.println(r.getAutore());
           }
        }