gpt4 book ai didi

java - 从异步任务类获取值到另一个类

转载 作者:行者123 更新时间:2023-12-02 11:46:30 24 4
gpt4 key购买 nike

This is the error

这是我的 asyncResponse.java

 public interface asyncResponse {

void processFinish(String output);
}

这是我的 Trialmenu.java,我想在其中检索我的值

abstract class trialmenu extends AppCompatActivity implements asyncResponse{
private ImageView logo;
private TextView status;

AsyncStatus asyncTask =new AsyncStatus();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newmenulayout);

logo = (ImageView) findViewById(R.id.imglogo);
status = (TextView) findViewById(R.id.txtStatus);

AsyncStatus.delegate = this;


}



void processFinish(String output) {
status.setText(output);


}
}

这是我的 AsyncStatus.java,出于隐私原因我没有发布我的 doInBackground

public class AsyncStatus extends AsyncTask<String, String, StringBuilder> {

public static asyncResponse delegate=null;

@Override
protected void onPostExecute(StringBuilder result) {
super.onPostExecute(result);

delegate.processFinish(result.toString());


}
}

最佳答案

您必须注册回调AsyncTask类。

 public class AsyncStatus extends AsyncTask<String, String, StringBuilder> {

public asyncResponse delegate=null;

public void setDelegate(asyncResponse delegate){
this.delegate=delegate;
}

@Override
protected void onPostExecute(StringBuilder result) {
super.onPostExecute(result);

delegate.processFinish(result.toString());


}
}

不要使用静态回调。您可以通过实现asyncResponseAnonymous 在调用类中实现。并且 processFinish() 实现必须是 public 并带有 @Override
注释。

abstract class trialmenu extends AppCompatActivity implements asyncResponse {
private ImageView logo;
private TextView status;
AsyncStatus asyncTask = new AsyncStatus();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newmenulayout);
logo = (ImageView) findViewById(R.id.imglogo);
status = (TextView) findViewById(R.id.txtStatus);
asyncTask.setDelegete(this);

}

@Override
public void processFinish(String output) {
// Callback
}
}

建议- 关注 naming convention in java使代码更具可读性。

关于java - 从异步任务类获取值到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48162178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com