Issue
This is my asyncResponse.java
public interface asyncResponse {
void processFinish(String output);
}
This is my trialmenu.java where i want to retrieve my value
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);
}
}
This is my AsyncStatus.java, I haven't posted my doInBackground for privacy reasons
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());
}
}
Solution
You have to register callback to AsyncTask
class.
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());
}
}
Do not use a static callback . And you can implement in your calling class by implementing asyncResponse
or by Anonymous
. And processFinish()
implementation has to be public with @Override
annotation.
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
}
}
Suggestion- Follow naming convention in java to make code more readable.
Answered By - ADM
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.