How to set the output of an asynchronous task to a textual representation of the main operation

public class  CheckLoginCredentialsTask extends AsyncTask<Boolean, Integer, 
EEmployeeHTTPResponse> {

MainActivity obj = new MainActivity();
private ProgressDialog ResponseProgress;
private Activity curActivity;
String employeeId,password;
String IMEI_Number = "";

public void setContextandActivity(Activity activity,String id,String pwd) {
    this.curActivity=activity;
    this.employeeId=id;
    this.password=pwd;
}

@SuppressLint("LongLogTag")
protected EEmployeeHTTPResponse doInBackground(Boolean... param) {
    try {
        HTTPCommunication loginHTTPRequest;
        if (employeeId != null) {
            Log.i("eEmp/CheckLoginTask", "doInBackg");
            loginHTTPRequest = new HTTPCommunication();
            EmployeeInfoDTO loginInfo = new EmployeeInfoDTO();
            Boolean Profile_Required_Status = param[0];
            loginHTTPRequest.setRequestType(EmpConstants.HTTPRequestType.NewUser);

            if (Profile_Required_Status) {  // Profile_Required_Status = Yes
                loginInfo.Profile_Required = EmpConstants.Profile_Required_Yes;
            }
            loginInfo.EMPID = employeeId;
            loginInfo.Password = password;
            return loginHTTPRequest.SendHTTPRequest(loginInfo);
        }
        return null;
    } catch (Exception e) {
        Log.d("eEmp/CheckLogin/Error", e.toString());
        return null;
    }
}

protected void onProgressUpdate(Integer... progress) {
    Log.d("eEmp/ChkLoginTskProg", "onProgress");
}

protected void onPostExecute(EEmployeeHTTPResponse result) {
    String toastStr = "";

    try {
        if (result != null) {
            if (result.HTTPStatusCode == 200) {
                if (result.Data != null) {
                    if (result.Data.ResponseCode == ActionStatus.Codes.UserFound) {
                        EmployeeInfoResponseDTO empDetails = (EmployeeInfoResponseDTO) result.Data.ActionResult;
                        if (empDetails != null) {
                            obj.tvEmpId.setText(empDetails.EmpID);
                            obj.tvType.setText(empDetails.EmpType);
                            obj.tvDept_code.setText(empDetails.DEPT_CODE);
                        }
                    }
                }
            }
        }
        Log.d("eEmp/CheckLoginTsk", "onPostExecute");
    } catch (Exception e) {
        Log.d("eEmp/PostExecute", e.toString());
    }
}
}

My result from the server is empDetailsin onPostExecute. As empDetailshas 3 fields EmpID, EmpType, EmpDept. Now I need to set these values ​​to textviewsin MainActivity.

I created an object for MainActivity, then I called obj.textviewBut it does not access textviewfrom MainActivity.

How can I access textviewhere. Or, if there are any other methods, tell me.

Any help would be appreciated.

+4
source share
6 answers

Try setting public static all textviewsand accessing it from this class directly usingMainActivity.obj.setText("");

0

textview MainActivity CheckLoginCredentialsTask textview MainActivity,

TextView tvEmpId,tvType,tvDept_code;


 CheckLoginCredentialsTask (Context context,String id,String pwd,TextView tvEmpId,TextView tvType,TextView tvDept_code)
  {
  this.context=context;
  this.employeeId=id;
  this.password=pwd;
  this.tvEmpId=tvEmpId;
  this.tvType=tvType;
  this.tvDept_code=tvDept_code;
  }

MainActivity,

MainActivity obj = new MainActivity();

TextView

 tvEmpId.setText(empDetails.EmpID);
 tvType.setText(empDetails.EmpType);
 tvDept_code.setText(empDetails.DEPT_CODE);

MainActivity

    TextView  tvEmpId=(TextView)findViewById(R.id.tvEmpId);
    TextView  tvType=(TextView)findViewById(R.id.tvType);
    TextView  tvDept_code=(TextView)findViewById(R.id.tvDept_code);

    CheckLoginCredentialsTask checkLoginCredentialsTask =new CheckLoginCredentialsTask (getApplicationContext(),"id","password",tvEmpId,tvType,tvDept_code);
    checkLoginCredentialsTask.execute();
0

EmpID, EmpType, EmpDept MainActivity .

:

public class AsyncHttpRequest extends AsyncTask<String, String, User>
{

    public AsyncResponse delegate = null;

    public interface AsyncResponse {
        void processFinish(User output);
    }


    public AsyncHttpRequest(Context con, AsyncResponse delegate){

        this.delegate = delegate;
        context =con;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }
    @Override
    protected User doInBackground(String... params) {
      return new User();
    }

    @Override
    protected void onPostExecute(User result) {

        delegate.processFinish(result);

    }

}

MainActivity:

 String url = "http:/URL";


        new AsyncHttpRequest(this,
                new AsyncHttpRequest.AsyncResponse() {

                    @Override
                    public void processFinish(User output) {
                        if (output != "error") {

                            output.getEmpID()
                            output.getEmpType()
                            output.getEmpDept()

                        }
                    }

                }).execute(url);
0

, obj, , , , , , , , MainActivity

MainActivity obj;

public void setContextandActivity(MainActivity activity,String id,String pwd) {
    this.obj=activity;
}
0

// post execute

Intent data = new Intent();
 [...]
if (getParent() == null) {
    setResult(Activity.RESULT_OK, data);
} else {
    getParent().setResult(Activity.RESULT_OK, data);
}

// , .

public void finish() {
    if (mParent == null) {
        int resultCode;
        Intent resultData;
        synchronized (this) {
            resultCode = mResultCode;
            resultData = mResultData;
        }
        if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken);
        try {
            if (ActivityManagerNative.getDefault()
                .finishActivity(mToken, resultCode, resultData)) {
                mFinished = true;
            }
        } catch (RemoteException e) {
            // Empty
        }
    } else {
        mParent.finishFromChild(this);
    }
}
0
Access onPostexecute method from your mainactivity as below:

    new CheckLoginCredentialsTask() {
                   @Override
                   protected void onPostExecute(EEmployeeHTTPResponse result) {
                       super.onPostExecute(result);
 try {
               if (result != null) {
                   if (result.HTTPStatusCode == 200) {
                       if (result.Data != null) {
                           if (result.Data.ResponseCode == ActionStatus.Codes.UserFound) {
                               EmployeeInfoResponseDTO empDetails = (EmployeeInfoResponseDTO) result.Data.ActionResult;
                               if (empDetails != null) {
                                   tvEmpId.setText(empDetails.EmpID);
                                   tvType.setText(empDetails.EmpType);
                                   tvDept_code.setText(empDetails.DEPT_CODE);
                               }
                           }
                       }
                   }
               }
               Log.d("eEmp/CheckLoginTsk", "onPostExecute");    } catch (Exception e) {
               Log.d("eEmp/PostExecute", e.toString());    }

      }.execute();
0

Source: https://habr.com/ru/post/1690229/


All Articles