简体   繁体   English

侦听器出错(尝试从具有接口的asynctask返回值)

[英]Error with Listeners (trying return a value from asynctask with interface)

I want to return a value from an asynctask to main ui (not a message). 我想从asynctask返回值到主ui(不是消息)。

The best and simple solution that I found it was https://github.com/levinotik/ReusableAsyncTask/tree/master/src/com/example from another post. 我发现它的最好,最简单的解决方案是另一篇文章中的https://github.com/levinotik/ReusableAsyncTask/tree/master/src/com/example

However the asynctask.setOnResultsListener(this) asFtp in my code call give me an error in Eclipse: 但是,我的代码调用中的asynctask.setOnResultsListener(this) asFtp在Eclipse中给我一个错误:

The method setOnResultsListener(ResultsListener) in the type MainActivity.asFTP is not applicable for the arguments ( new View.OnClickListener(){} ) MainActivity.asFTP类型的方法setOnResultsListener(ResultsListener)不适用于自变量( new View.OnClickListener(){}

I think suppose/imagine the problem, but how solves it in the simplest way? 我认为是设想/设想问题,但是如何以最简单的方式解决呢?

Button btn_copiar = (Button) findViewById(R.id.btn_copiar);
btn_copiar.setOnClickListener(new OnClickListener(){
public void onClick (View v) {
    try {
        asFtp = new asFTP();
        if (comprobar_preferencias()){
        Log.d(TAG, prefServidor + " " + String.valueOf(prefPuerto) + " " + prefUsuario + " " + prefPass);
        asFtp.setOnResultsListener(this); // here I get the eclipse error
        asFtp.execute();

Thanks. 谢谢。

First, when you reference this inside the creation of a new object, this refers to that object. 首先,当您在创建新对象时引用对象时, 是指该对象。

You are saying that the onClickListener is the ResultListener. 您是说onClickListener是ResultListener。 I am going to guess that the class in which you create the onClickListener is the class that implements the onResultListener interface. 我猜您在其中创建onClickListener的类是实现onResultListener接口的类。

So for example if you have 例如,如果您有

public class MyClass {
    private int test;

    public void example() {
         // this refers to MyClass here (the current scope)
         this.test = 0;
         OnClickListener listener = new View.OnClickListener() {
              // Now we are in the intalization of a new Object.
              // "this" refers to the OnClickListener that you are creating
         }
    }
}

So for example, in this case, if you wanted to refer to MyClass in the creation block of the OnClickListener, you simply need to reference the name of the class. 因此,例如,在这种情况下,如果要在OnClickListener的创建块中引用MyClass,则只需引用该类的名称。 So for example: 因此,例如:

public class MainActivity extends Activity implements OnReultsListener {
    private int test;

    public void example() {
         this.test = 0;
         OnClickListener listener = new View.OnClickListener() {
               asFtp = new asFTP();
               asFtp.setOnClickListener(MainActivity.this)'
               ... whatever else you need ...
         }
    }
}

In your case, you would have something like 在您的情况下,您会遇到类似

Button btn_copiar = (Button) findViewById(R.id.btn_copiar);
btn_copiar.setOnClickListener(new OnClickListener(){
public void onClick (View v) {
   try {
       asFtp = new asFTP();
       if (comprobar_preferencias()){
          Log.d(TAG, prefServidor + " " + String.valueOf(prefPuerto) + " " + prefUsuario + " " + prefPass);
    asFtp.setOnResultsListener(MainActivity.this); // here I get the eclipse error
    asFtp.execute();

EDIT: I know this example is crude, I just wanted to get the point across. 编辑:我知道这个例子很粗糙,我只是想表达一点。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM