简体   繁体   中英

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);

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); 
public View getView(){
View v;
LayoutInflater inflater = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl = (RelativeLayout)findViewById(R.id.rlmain);
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW,etSearch);

v = inflater.inflate(R.layout.buttons, null);
mStreaming = (Button)v. findViewById(R.id.btnStreamingapps);
mcustom = (Button)v.  findViewById(R.id.btncustom);
mUtil = (Button)v.  findViewById(R.id.btnutil);
mplayers = (Button)v.  findViewById(R.id.btnplayers);
 rl.addView(v,lp);


return v;
}

this was what i did for getview() @cricket_007 is there away to make a listview which is in the main activity go below this view at the moment it goes through it lol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM