[英]How do I pass parameters/argument to a callback function?
A naive question as I am a bit new to programming.一个天真的问题,因为我对编程有点陌生。 I am Working on an Android application where I need to pass parameters to the callback method(Not sure if the verbiage is right).
我正在开发一个 Android 应用程序,我需要将参数传递给回调方法(不确定措辞是否正确)。
I want the parameters/variables to be available to the Main Function.我希望参数/变量可用于主 Function。 I am calling the Caller to invoke my main function and hence need the parameters to pass from the Caller.
我正在调用调用者来调用我的主要 function,因此需要从调用者传递参数。
something like the classic functions does类似于经典功能的东西
Example例子
method(param1, param2);
function method(param1, param2){
Log(param1 + param 2);
....
}
I need the below code to achieve the functionality as the above example我需要下面的代码来实现上面例子的功能
the code is as follows:代码如下:
//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);
}
//Main function: I want some parameters to be passed to this method //main function: 我想给这个方法传递一些参数
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 ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
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) {}
});
}
Please help...请帮忙...
Based on what i understand.根据我的理解。 Basically you want to pass something to a callback method.
基本上,您想将某些内容传递给回调方法。
I would suggest to create another class that will implement ValueEventListener
.我建议创建另一个 class 来实现
ValueEventListener
。
Like so像这样
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
}
}
To use above class,要使用上述 class,
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));
}
Hopefully this helps希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.