简体   繁体   English

如何将参数/参数传递给回调 function?

[英]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.

相关问题 如何将Java函数传递给String []参数? - How do I pass a Java function a String[] argument? 将函数参数传递给Java中的回调函数 - Pass function argument to callback function in Java 如何编写从服务器端 JavaScript 调用的 Java 方法,该方法将 JS 回调函数作为参数? - How do I write a Java method, to be called from server side JavaScript, that takes a JS callback function as an argument? 如何将变量参数传递给instanceof? - How do I pass a variable argument to instanceof? 如何使用Guice将参数传递给Provider? - How do I pass parameters into a Provider with Guice? 如何将参数传递给Windward Reports? - How do I pass parameters to Windward Reports? 在使用 JAR 运行 spark-submit 时,如何将程序参数传递给主函数? - How do I pass program-argument to main function in running spark-submit with a JAR? 如何将参数传递给Google App Engine Servlet? - How do I pass an argument to a Google App Engine servlet? 如何通过spring表达式语言评估作为参数? - How do I pass a spring expression language evaluation as an argument? 如何将CLI参数传递给Cucumber Java测试服? - How do I pass a CLI argument to a Cucumber Java test suit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM