如何在不监听事件的情况下从firebase数据库中检索数据

时间:2018-03-29 06:55:42

标签: android firebase android-fragments firebase-realtime-database android-mvp

您好我正在为我的Android应用程序使用Firebase数据库。我将我的数据存储在firebase数据库中,我想在每次创建片段时获取它但是当我启动我的应用程序时,该方法(从firebase数据库获取数据)返回空值 每当我用android设备恢复片段时,recyclelerView就会填充来自firebase的数据。 我的问题是列表第一次返回null的原因。如何在创建片段时获取数据

OffersFragment

 @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View offersView = inflater.inflate(R.layout.fragment_offers, container, false);

    mOffersRecyclerView = offersView.findViewById(R.id.rv_offers);
    mAddCard = offersView.findViewById(R.id.fab_add_card);

    setUp();

    return offersView;
}

@Override
public void setPresenter(OffersContract.Presenter presenter) {
    mPresenter = presenter;
}

@Override
public void setUp() {
    mOffersList = mPresenter.getOffersList();
    mOffersAdapter = new OffersAdapter(getActivity(), mOffersList);

    mOffersRecyclerView.setAdapter(mOffersAdapter);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mOffersRecyclerView.setLayoutManager(layoutManager);
    mOffersRecyclerView.setHasFixedSize(false);

    performActions(mAddCard);
}

OffersPresenter

 @Override
public List<Offers> getOffersList() {

    return databaseHelper.getOffers();
}

DatabaseHelper - &gt; getOffers()

public List<Offers> getOffers() {
    DatabaseReference offerReference = getDatabase().child("offers");

    offerReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            for (DataSnapshot d : children) {
                offersList.add(d.getValue(Offers.class));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return offersList;
}

2 个答案:

答案 0 :(得分:1)

firebase回调是异步的,因此方法getOffers在通过回调更新之前将返回null。 您必须将适配器实例和列表实例传递给firebase回调,以便从那里进行更新 试试这个机制: 在您的活动中初始化您的列表

@Override
public void setUp() {
    mOffersList = new ArrayList<Offers> ();
    mOffersAdapter = new OffersAdapter(getActivity(), mOffersList);

    mOffersRecyclerView.setAdapter(mOffersAdapter);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mOffersRecyclerView.setLayoutManager(layoutManager);
    mOffersRecyclerView.setHasFixedSize(false);

    performActions(mAddCard);
}

更新你的getter

@Override
public void getOffersList(List<Offers> offers, OffersAdapter adapter) {

    return databaseHelper.getOffers(offers, adapter);
}

助手将是这样的:

public void getOffers(List<Offers> offers,OffersAdapter adapter) {
    if (offers == null) offers = new ArrayList<Offers> ();
    DatabaseReference offerReference = getDatabase().child("offers");

    offerReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            for (DataSnapshot d : children) {
                offers.add(d.getValue(Offers.class));
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

请记住,您正在使用值事件,如果任何数据发生更改,将会调用该事件,因此数据将被多次添加。如果您有任何问题,请使用单值事件或子事件侦听器来解决问题。

答案 1 :(得分:1)

它返回null,因为您将getOffers()放在void的末尾删除return语句并生成commit() onDataChange(DataSnapshot dataSnapshot)并添加逻辑以获取/更新数据和/或onDataChange(DataSnapshot dataSnapshot) import tensorflow as tf # Mx = b M = tf.Variable([[1,2,3],[4,5,6],[7,8,9]],validate_shape=False) x = tf.Placeholder(shape=(None, 3)) b = tf.matmul(M,x) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(sess.run(b)) print(sess.run(b, feed_dict = {x:[[1],[2],[3]]})) 内的daterangepicker片段,因此它会在第一次提取数据时返回您的列表。

我通常做的是提交片段或在其中设置其参数 val()