markharrop markharrop -4 years ago 64
Android Question

Reusing a ValueEventListener firebase

hi guys i have a ValueEventlistener class that listens for a value of 1 when the value is reached it changes the look of the buttons in my main activity. All works fine. But ive been informed that the way im using the listener is wrong. because i recall the same instance over and over

public class getRewards {
Firebase mListref;
Button mstreaming,mcustom,mlauncher,mmovies;
Context mcontext;

public getRewards(Firebase ListRef, Context context){
this.mListref = ListRef;
this.mcontext = context;
}
public void getButtonRewards(Button a,Button b,Button c,Button d){
this.mstreaming = a;
this.mcustom = b;
this.mlauncher = c;
this.mmovies = d;


//HERE IS WHERE I REUSE THE LISTENER IS THIS THE RIGHT WAY TO USE IT??

RewardValueListener reward1 = new RewardValueListener();
mListref.child("RewardsSystem").addValueEventListener(reward1);
reward1.getButton(mstreaming,mcontext);
RewardValueListener reward2 = new RewardValueListener();
mListref.child("RewardsSystem").addValueEventListener(reward2);
reward2.getButton(mcustom,mcontext);
RewardValueListener reward3 = new RewardValueListener();
mListref.child("RewardsSystem").addValueEventListener(reward3);
reward3.getButton(mlauncher,mcontext);
RewardValueListener reward4 = new RewardValueListener();
mListref.child("RewardsSystem").addValueEventListener(reward4);
reward4.getButton(mmovies,mcontext);
}


}
what is the best way to reuse the listener to change all my buttons instead of just one or doing it the above way?

my Reward listener class

public class RewardValueListener implements ValueEventListener {

FirebaseAuth mAuth;
String userid;
int i;
View view;
String dls;
Button mButton;
Context c;

public RewardValueListener() {

}

@Override
public void onDataChange(DataSnapshot dataSnapshot) {

mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
userid = user.getUid();
dls = dataSnapshot.child(userid).child("counter").getValue(String.class);

i = Integer.parseInt(dls);
if (i == 1) {

mButton.setBackground(c.getResources().getDrawable(R.drawable.buttonbkground2));

}
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}

public void getButton(Button button,Context context) {
mButton = button;
c=context;

}


and finally how i call it on my main activity

getRewards checkRewardStatus = new getRewards(mListRef,mContext);
checkRewardStatus.getButtonRewards(mStreaming,mcustom,mUtil,mplayers);

Answer Source

If I understand correctly, you just want to encapsulate four buttons into a single object.

That would best be done using the View that holds them all.

public class Rewards {
    private Firebase mListref;
    private final Context mcontext;
    Button mstreaming,mcustom,mlauncher,mmovies;

    public Rewards(Context context, Firebase ref, View rootView){
        this.mListref = ref;
        this.mcontext = context;

        if (rootView != null) {
             this.mstreaming = (Button) rootView.findViewById(R.id.streaming);
            this.mcustom = (Button) rootView.findViewById(R.id.custom);
            this.mlauncher = (Button) rootView.findViewById(R.id.launcher);
            this.mmovies = (Button) rootView.findViewById(R.id.movies);
        } 
    }
}

How you create this depends on an Activity / Fragment situation, but generally, new Rewards(getContext(), firebaseRef, getView()). (Those methods won't all resolve, but I don't know how you are using this.)

Then, same as before, add to the constructor.

public class RewardValueListener implements ValueEventListener {

    private FirebaseAuth mAuth;
    private Rewards mRewards;

    public RewardValueListener(Rewards rewards) {
        this.mRewards = rewards; 
    }

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        mAuth = FirebaseAuth.getInstance();
        String userId = mAuth.getCurrentUser().getUid();
        Integer counter = dataSnapshot.child(userid).child("counter").getValue(Integer.class);

        if (counter == 1) {
            // mRewards.mstreaming.setBackground( ... );  
        } else if (counter == 2) {
            // mRewards.mcustom.setBackground( ... ); 
        }
    }

Not sure what conditions your buttons are expected to be changing.

And to wrap-up.

View rootView = getView(); // TODO: Implement this somehow
Rewards rewards = new Rewards(mContext, mListref, rootView);
RewardValueListener listener = new RewardValueListener(rewards);
mListref.child("RewardsSystem").addValueEventListener(listener);

In an Activity, you don't really need a method, and can instead use this

View rootView = findViewById(android.R.id.content); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download