没有调用addValueEventListener

时间:2018-01-30 23:31:26

标签: java android firebase firebase-realtime-database

我正在尝试使用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:在循环中”+ dataSnapshot1.toString());不被称为。即使是log.d(TAG,“On data cange”+ datasnapshot)也没有被调用,这意味着存在问题。有人请帮帮我

1 个答案:

答案 0 :(得分:1)

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