简体   繁体   English

如何从具有多个节点的 firebase 中检索数据?

[英]How retrieve data from firebase with multiple nodes?

I can't resolve this problem: When i try to retrieve data from firebase, it says that there are not setter/field for [Node name] on class [Class name].我无法解决这个问题:当我尝试从 firebase 检索数据时,它说 class [类名称] 上没有 [节点名称] 的设置器/字段。

My class is "Ricetta" and Every node is named with the name of ricetta.我的 class 是“Ricetta”,每个节点都以 ricetta 的名字命名。 Firebase 结构

Ricetta.class Ricetta.class

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

}

OnCreateView method of my HomeFragment.java我的 HomeFragment.java 的 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;
}

When i run this is shown:当我运行时显示:

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

You're attaching a ChildEventListener on /users .您在/users上附加了ChildEventListener This means that your onChildAdded is called with the direct child nodes of users, which is m50... in your screenshot.这意味着您的onChildAdded是使用用户的直接子节点调用的,即m50...在您的屏幕截图中。 Then you read the value of the ricette child under there, which leads to all the 9999 , dfsdfdf , etc nodes under there.然后你读取那里的ricette child 的值,这会导致那里的所有9999dfsdfdf等节点。

My guess it that you're trying to read one or all child nodes under ricette , for which you'll need to loop over the child nodes of the snapshot with:我猜您正在尝试读取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());
       }
    }
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());
        
       }
    }
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM