簡體   English   中英

如何從具有多個節點的 firebase 中檢索數據?

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

我無法解決這個問題:當我嘗試從 firebase 檢索數據時,它說 class [類名稱] 上沒有 [節點名稱] 的設置器/字段。

我的 class 是“Ricetta”,每個節點都以 ricetta 的名字命名。 Firebase 結構

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

}

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

當我運行時顯示:

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

您在/users上附加了ChildEventListener 這意味着您的onChildAdded是使用用戶的直接子節點調用的,即m50...在您的屏幕截圖中。 然后你讀取那里的ricette child 的值,這會導致那里的所有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());
       }
    }
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