gpt4 book ai didi

java - 未调用 addValueEventListener

转载 作者:行者123 更新时间:2023-11-30 00:08:41 25 4
gpt4 key购买 nike

我正在尝试使用 Firebase 获取存储在我的数据库中的数据,但未调用 addValueEventListener。下面是我的数据库的样子:

How my database Looks like

下面是我的规则:

My rules

下面是我的代码:

package com.example.user.equichange;

 import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.StreamDownloadTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;





public class NoAddOns_Tab_fragment extends Fragment {
RecyclerView recyclerView;
Adapter adapter;
List<ITEM> list;
String TAG = "TAG";

Firebase firebasedb;
DatabaseReference mdatabaseref;

public NoAddOns_Tab_fragment() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_no_add_ons,container,false);


return view;
}

@Override
public void onViewCreated( View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView_no_Top_ups);
mdatabaseref = FirebaseDatabase.getInstance().getReference().child("Users");
Log.d(TAG, "onViewCreated: "+mdatabaseref.getRoot());

mdatabaseref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: " + dataSnapshot);
Log.d(TAG,dataSnapshot.getChildren().toString());

list = new ArrayList<ITEM>();

for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: In the loop " + dataSnapshot1.toString());


ITEM value = dataSnapshot1.getValue(ITEM.class);
ITEM itemModel = new ITEM();

String Title = value.getTitle();
Log.d(TAG, "onDataChange:Title "+ value.getTitle());



String Description = value.getDescription();
String Amount = value.getAmount();

itemModel.setTitle(Title);
itemModel.setDescription(Description);
itemModel.setAmount(Amount);

list.add(itemModel);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled: Error");

}
});

// Log.d(TAG, "LIST "+ list);


Adapter adapter = new Adapter(getContext(),list);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
recyclerView.setAdapter(adapter);
}

我的 firebase 已正确设置,因为我可以将数据发送到 Firebase 数据库:

从我的 logcat 你可以看到 Log.d(TAG, "onDataChange: In the loop "+ dataSnapshot1.toString());没有被调用。即使是 log.d(TAG,"On data cange"+ datasnapshot) 也没有被调用,这意味着有问题..有人可以帮我吗

最佳答案

获取值时无需遍历任何内容。 ValueEventListener 会为您完成这项工作。因此,摆脱 for 循环并仅使用 ITEM value = dataSnapshot.getValue(ITEM.class); 应该适合您。快速提问,您是否使用 ITEM 类将数据存储在 Firebase 中?如果是这样,这个解决方案应该有效。否则,您需要使用与设置数据相同的类来从 Firebase 检索数据。

关于java - 未调用 addValueEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com