如何将参数传递给回调函数?

huangapple 未分类评论52阅读模式
英文:

How do I pass parameters/argument to a callback function?

问题

以下是翻译好的部分:

一个天真的问题,因为我对编程有点新手。我正在开发一个 Android 应用程序,需要将参数传递给回调方法(不确定用词是否正确)。

我希望这些参数/变量在主函数中可用。我正在调用调用者来调用我的主函数,因此需要从调用者传递参数。

就像经典函数所做的那样:

示例

method(param1, param2);
  
function method(param1, param2){
    Log(param1 + param2);
      ....
}

我需要以下代码来实现与上面示例相同的功能。

代码如下:

// 调用者:
getChioceList(new MyCallback() {
    @Override
    public void onCallback(ArrayList<String> value) {
        Log.d("TAG", "Config CallBack " + value);
    }
});
    
// 接口
public interface MyCallback{
    void onCallback(ArrayList<String> value);
}

// 主函数:我希望向这个方法传递一些参数

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) {}
    });
}

请帮忙...

英文:

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).

I want the parameters/variables to be available to the Main Function. I am calling the Caller to invoke my main function and hence need the parameters to pass from the Caller.

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&lt;String&gt; value) {
                Log.d(&quot;TAG&quot;, &quot;Config CallBack &quot; + value);
                }
});
    
//Interface
public interface MyCallback{
    void onCallback(ArrayList&lt;String&gt; value);
}

//Main function : I want some parameters to be passed to this method

 public void getChioceList(final MyCallback myCallback) {
     final ArrayList &lt; String &gt; result = new ArrayList &lt; &gt; ();
     final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
     Query query = ref.child(&quot;Device&quot;).orderByChild(&quot;home&quot;).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, &quot;Config: get Input 3 &quot; + result);
             }
             myCallback.onCallback(result);
         }

         @Override
         public void onCancelled(@NonNull DatabaseError databaseError) {}
     });
 }

Please help...

答案1

得分: 0

基于我理解的内容。基本上您想要将一些内容传递给回调方法。

我建议创建另一个类来实现 ValueEventListener

像这样:

public class SomeClassThatWillImplement implements ValueEventListener {

    private String cVar1;
    private String cVar2;

    public SomeClassThatWillImplement(String param1, String param2) {
        this.cVar1 = param1;
        this.cVar2 = param2;
    }

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        // 使用 this.cVar1 做一些事情
        // 使用 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) {
        // 使用 this.cVar1 做一些事情
        // 使用 this.cVar2 做一些事情
    }

}

要使用上面的类,

public void getChoiceList(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));
}

希望这有所帮助。

英文:

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.

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, &quot;Config: get Input 3 &quot; + 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,

public void getChioceList(final MyCallback myCallback) {
    final ArrayList&lt;String&gt; result = new ArrayList&lt;&gt;();
    final DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    Query query = ref.child(&quot;Device&quot;).orderByChild(&quot;home&quot;).equalTo(homeID);
    query.addListenerForSingleValueEvent(new SomeClassThatWillImplement(param1, param2));
}

Hopefully this helps

huangapple
  • 本文由 发表于 2020年4月10日 22:40:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61142643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定