大家好我有一个ValueEventlistener类,当达到该值时会侦听值1,它会改变主活动中按钮的外观。一切正常。但香港专业教育学院被告知我使用听众的方式是错误的。因为我一遍又一遍地回忆起同一个实例
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;
//在这里,我重新使用听众,这是使用它的正确途径吗?
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);
}
} 什么是重用侦听器来更改所有按钮而不是仅仅按一个或按上述方式执行操作的最佳方法?
我的奖赏听众课
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;
}
最后我在主要活动中如何称呼它
getRewards checkRewardStatus = new getRewards(mListRef,mContext);
checkRewardStatus.getButtonRewards(mStreaming,mcustom,mUtil,mplayers);
答案 0 :(得分:0)
如果我理解正确,您只想将四个按钮封装到一个对象中。
最好使用保留所有内容的View
来完成。
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);
}
}
}
如何创建这取决于活动/片段情况,但通常是new Rewards(getContext(), firebaseRef, getView())
。 (这些方法都没有解决,但我不知道你是如何使用它的。)
然后,和之前一样,添加到构造函数中。
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( ... );
}
}
不确定您的按钮预期会发生什么变化。
总结。
View rootView = getView(); // TODO: Implement this somehow
Rewards rewards = new Rewards(mContext, mListref, rootView);
RewardValueListener listener = new RewardValueListener(rewards);
mListref.child("RewardsSystem").addValueEventListener(listener);
在一项活动中,您并不真正需要一种方法,而是可以使用此
View rootView = findViewById(android.R.id.content);
答案 1 :(得分:0)
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;
}
这就是我为getview()@ cricket_007所做的 有没有在主要活动中制作一个列表视图,在它通过它的那一刻就在这个视图下方大声笑