从其活动之外找到按钮 [英] Locating a button from outside its activity
问题描述
大家好,我有Firebaselistener类,它侦听值1.当达到此值时,我希望按钮drawable更改.最初,我在主要活动中设置了所有这些功能,并且使用
Hi guys i have Firebaselistener class and it listens for the value of 1. when this value is reached i want the button drawable to change. Originally i had this all set up in my main activity and it worked ok using
if(dls==1){
m.setBackground(getResources().getDrawable(R.drawable.buttonbkground2));
}
在我的主要活动中,使用valueEventlistener并监听该值.
from with in my main activity using a valueEventlistener and listening for the value.
但是我现在设置了一个类来做同样的事情,我无法从主活动中访问按钮ID.
but now ive set up a class to do the same thing i cant access the button ids from the main activity.
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);
Log.d("tag", "Value: " + dls);
i = Integer.parseInt(dls);
if (i == 1) {
Log.d("tag", "Congrats " + i);
mButton.setBackground(c.getResources().getDrawable(R.drawable.buttonbkground2));
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
***包含Code答案的代码编辑
***Code edit with crickets answer included
public void getButton(Button button,Context context) {
mButton = button;
c=context;
}
我知道与调用视图有关,但是在哪里调用它呢?
i know its something to do with calling the view but how n where do i call it?
有人可以帮助我,告诉我我哪里出问题了吗
can somebody please help me and tell me where im going wrong thank you
***编辑好的,感谢@ cricket_007的回答,效果很好.现在,我可以执行代码了,可以调用Listener类来更改主要活动中的所有按钮,而不仅仅是一个按钮.
***Edit ok so thanks to @cricket_007 for his answer which works great. now i have the code working is there away to call my Listener class to change all the buttons in my main activity rather than just one.
如果按这种方式编写代码,对于每个按钮,我都能使它正常工作,但是有一种方法可以压缩它,这样我就不必溢出我的主要活动
i can get it to work if for every button if write the code this way but is there a way to condense it so i dont have to overflow my main activity
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);
ect ect....
推荐答案
只需添加到构造函数中即可.
Just add to the constructor.
final Button mButton;
final Context mContext;
public RewardValueListener(Context context, Button button) {
mContext = context;
mButton = button;
}
并使用该按钮代替 findViewById
和 mContext.getResources()
注意:创建此类时,您需要有一个可用按钮
Note: you'll need to have a button available when you create this class
这篇关于从其活动之外找到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!