Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

android - 'addListenerForSingleValueEvent' in populateViewHolder method of FirebaseRecyclerAdapter gets called whenever data changes under Firebase reference

I'm using FirebaseRecyclerAdapter in my app and trying to populate it by fetching some data inside it's populateViewHolder() method and then assigning values to different parameters.

Here's my code:

private void attachRecyclerViewAdapter() {
        Query lastFifty = aReference.child(requestID).limitToFirst(50);
        mRecyclerViewAdapter = new FirebaseRecyclerAdapter<AModelClass, AModelClass.ViewHolder>(
                AModelClass.class, R.layout.a_layout, AModelClass.ViewHolder.class, lastFifty) {

            @Override
            protected void populateViewHolder(final AModelClass.ViewHolder viewHolder, AModelClass model, int position) {
                final String key = this.getRef(position).getKey();
                aReference.child(requestID).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() != null) {    
                            aReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    if (dataSnapshot.getValue() != null) {

                                        if (dataSnapshot.hasChild("pName") && dataSnapshot.hasChild("pUrl") && dataSnapshot.hasChild("currentLat") && dataSnapshot.hasChild("currentLng")) {
                                            final Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                                            pA = map.get("pName");
                                            uidP = map.get("pUrl");
                                            nP.add(map.get("pName"));

                                            cLatPlayers.add(map.get("currentLat").trim());
                                            cLngPlayers.add(map.get("currentLng").trim());

                                            viewHolder.setPNameT(pA);
                                            viewHolder.setPicP(uidP);
                                            viewHolder.setCurrentLatAU(String.valueOf(currentLtAU));
                                            viewHolder.setCurrentLngAU(String.valueOf(currentLnAU));

                                            addMarkers();

                                            layout.setVisibility(View.VISIBLE);
                                            progressBar.setVisibility(View.INVISIBLE);
                                        }

                                    } else {
                                    }

                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });

                        } else {
                            Toast.makeText(getBaseContext(), "no data available yet", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        };

        mRecyclerViewAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeRemoved(int positionStart, int itemCount) {
            }

            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                super.onItemRangeInserted(positionStart, itemCount);
            }

            @Override
            public void onItemRangeChanged(int positionStart, int itemCount) {
                super.onItemRangeChanged(positionStart, itemCount);
            }
        });

        aList.setAdapter(mRecyclerViewAdapter);
    }

attachRecyclerViewAdapter() is placed in onCreate().

Here's aList initialisation in onCreate():

aList = (RecyclerView) findViewById(R.id.aList);
aList.setLayoutManager(nLinearLayoutManager);

Here's onLocationChanged() method:

@Override
public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        currentLtAU = mCurrentLocation.getLatitude();
        currentLnAU = mCurrentLocation.getLongitude();

        aReference.child(requestID).child(userID).child("currentLatUpdated").setValue(String.valueOf(currentLtAU));
        aReference.child(requestID).child(userID).child("currentLngUpdated").setValue(String.valueOf(currentLnAU));        
}

which saves updated lat and lng to database whenever location is changed under the same reference from where data is getting retrieved above.

The LocationRequest code:

protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(500);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(0);
}

Everything is working just fine here, the only problem is that the code under reference: aReference.child(requestID).child(key) or (aReference.child(requestID) maybe?) is getting called again and again (though it is supposed to be called just once because of addListenerForSingleValueEvent), thus filling nP, cLatPlayers, cLngPlayers multiple times and also the addMarkers() method is getting called multiple times causing problem in the Map.

Why is this happening and How can I make sure that this doesn't gets called multiple times?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...