Java 如何将异步任务的输出设置为主活动的文本视图 public类CheckLoginCredentialsTask扩展异步任务{ MainActivity obj=新的MainActivity(); 私有进程对话响应进程; 私人活动; 字符串employeeId,密码; 字符串IMEI_Number=“”; public void setContextandActivity(活动活动、字符串id、字符串pwd){ 这就是:活动性=活动性; 这个.employeeId=id; this.password=pwd; } @SuppressLint(“LongLogTag”) 受保护的EEmployeeHTTPResponse doInBackground(布尔…参数){ 试一试{ httpcommunicationloginhttprequest; if(employeeId!=null){ Log.i(“eEmp/CheckLoginTask”、“doInBackg”); loginHTTPRequest=新的HTTPCommunication(); EmployeeInfoDTO loginInfo=新EmployeeInfoDTO(); 布尔配置文件_必需_状态=参数[0]; loginHTTPRequest.setRequestType(EmpConstants.HTTPRequestType.NewUser); 如果(Profile\u Required\u Status){//Profile\u Required\u Status=Yes loginInfo.Profile_Required=EmpConstants.Profile_Required_Yes; } loginInfo.EMPID=员工ID; loginInfo.Password=密码; 返回loginHTTPRequest.SendHTTPRequest(loginInfo); } 返回null; }捕获(例外e){ Log.d(“eEmp/CheckLogin/Error”,例如toString()); 返回null; } } 受保护的void onProgressUpdate(整数…进度){ 日志d(“eEmp/ChkLoginTskProg”、“onProgress”); } PostExecute上受保护的void(EEmployeeHTTPResponse结果){ 字符串toastStr=“”; 试一试{ 如果(结果!=null){ 如果(result.HTTPStatusCode==200){ 如果(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); 对象tvDept_代码.setText(empDetails.DEPT_代码); } } } } } Log.d(“eEmp/CheckLoginTsk”、“onPostExecute”); }捕获(例外e){ Log.d(“eEmp/PostExecute”,例如toString()); } } }
我从服务器得到的结果是Java 如何将异步任务的输出设置为主活动的文本视图 public类CheckLoginCredentialsTask扩展异步任务{ MainActivity obj=新的MainActivity(); 私有进程对话响应进程; 私人活动; 字符串employeeId,密码; 字符串IMEI_Number=“”; public void setContextandActivity(活动活动、字符串id、字符串pwd){ 这就是:活动性=活动性; 这个.employeeId=id; this.password=pwd; } @SuppressLint(“LongLogTag”) 受保护的EEmployeeHTTPResponse doInBackground(布尔…参数){ 试一试{ httpcommunicationloginhttprequest; if(employeeId!=null){ Log.i(“eEmp/CheckLoginTask”、“doInBackg”); loginHTTPRequest=新的HTTPCommunication(); EmployeeInfoDTO loginInfo=新EmployeeInfoDTO(); 布尔配置文件_必需_状态=参数[0]; loginHTTPRequest.setRequestType(EmpConstants.HTTPRequestType.NewUser); 如果(Profile\u Required\u Status){//Profile\u Required\u Status=Yes loginInfo.Profile_Required=EmpConstants.Profile_Required_Yes; } loginInfo.EMPID=员工ID; loginInfo.Password=密码; 返回loginHTTPRequest.SendHTTPRequest(loginInfo); } 返回null; }捕获(例外e){ Log.d(“eEmp/CheckLogin/Error”,例如toString()); 返回null; } } 受保护的void onProgressUpdate(整数…进度){ 日志d(“eEmp/ChkLoginTskProg”、“onProgress”); } PostExecute上受保护的void(EEmployeeHTTPResponse结果){ 字符串toastStr=“”; 试一试{ 如果(结果!=null){ 如果(result.HTTPStatusCode==200){ 如果(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); 对象tvDept_代码.setText(empDetails.DEPT_代码); } } } } } Log.d(“eEmp/CheckLoginTsk”、“onPostExecute”); }捕获(例外e){ Log.d(“eEmp/PostExecute”,例如toString()); } } },java,android,Java,Android,我从服务器得到的结果是empDetails中的onPostExecute方法。在empDetails中有3个字段EmpID,EmpType,EmpDept。现在我需要将这些值设置为main活动中的textview 我为MainActivity创建了对象,然后调用了obj.textview,但它没有从MainActivity访问textview 如何访问此处的textview。或者如果还有其他方法,请告诉我 任何帮助都将不胜感激。将textview对象从MainActivity的构造函数中传递给c
empDetails
中的onPostExecute
方法。在empDetails
中有3个字段EmpID
,EmpType
,EmpDept
。现在我需要将这些值设置为main活动
中的textview
我为MainActivity
创建了对象,然后调用了obj.textview
,但它没有从MainActivity
访问textview
如何访问此处的textview
。或者如果还有其他方法,请告诉我
任何帮助都将不胜感激。将
textview
对象从MainActivity
的构造函数中传递给checkLoginRedentialTask
,并在MainActivity
中创建textview
的对象,如下所示
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());
}
}
}
不要像这样创建对象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 obj = new MainActivity();
在主活动中
tvEmpId.setText(empDetails.EmpID);
tvType.setText(empDetails.EmpType);
tvDept_code.setText(empDetails.DEPT_CODE);
尝试设置所有
textviews
的公共静态视图,并使用MainActivity.obj.setText(“”)直接从此类访问它代码>您应该将EmpID、EmpType、EmpDept返回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();
问题是您引用的obj不是您的实际活动,它只是您自己创建的一个实例,您需要将其分配给传入的值,并且您传递的引用必须是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);
//在post execute上尝试此功能
MainActivity obj;
public void setContextandActivity(MainActivity activity,String id,String pwd) {
this.obj=activity;
}
//在您想要使用的活动中编写此代码
Intent data = new Intent();
[...]
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
请根据我的要求修改您的答案。现在可以了,响应是一个用户对象。在您的doInBackground方法中返回用户对象我不理解您的答案@kamal vermaPlease请帮助我获得正确答案。我将通过以下步骤:MainActivity.tvEmpId.setText(empDetails.DEPT_代码)非常感谢。向飞越者致敬@尼希尔·索兰基·尼鲁
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);
}
}
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();