一个天真的问题,因为我对编程有点陌生。 我正在一个Android应用程序上工作,我需要将参数传递给回调方法(不确定该单词是否正确)。
我希望参数/变量对主要功能可用。我正在调用调用方以调用我的主要功能,因此需要从调用方传递参数。
类似经典功能的东西
示例
log(n)
我需要以下代码来实现上述示例的功能
代码如下:
method(param1, param2);
function method(param1, param2){
Log(param1 + param 2);
....
}
// Main函数:我希望将一些参数传递给此方法
//Caller:-
getChioceList(new MyCallback() {
@Override
public void onCallback(ArrayList<String> value) {
Log.d("TAG", "Config CallBack " + value);
}
});
//Interface
public interface MyCallback{
void onCallback(ArrayList<String> value);
}
请帮助...
答案 0 :(得分:0)
根据我的理解。基本上,您想将某些内容传递给回调方法。
我建议创建另一个将实现ValueEventListener
的类。
像
public class SomeClassThatWillImplement implements ValueEventListener {
private String cVar1;
private String cVar2;
public SomeClassThatWillImplement (String param1, String parma2) {
this.cVar1 = param1;
this.cVar2 = param2;
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Do something with this.cVar1
//Do something with this.cVar2
for (DataSnapshot dataSnapshots : dataSnapshot.getChildren()) {
result.add(dataSnapshots.getKey());
Log.i(TAG, "Config: get Input 3 " + result);
}
myCallback.onCallback(result);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//Do something with this.cVar1
//Do something with this.cVar2
}
}
要使用上述课程,
public void getChioceList(final MyCallback myCallback) {
final ArrayList<String> result = new ArrayList<>();
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query query = ref.child("Device").orderByChild("home").equalTo(homeID);
query.addListenerForSingleValueEvent(new SomeClassThatWillImplement(param1, param2));
}
希望这对您有帮助