我是 android 的新手。我有一个用 Activity 扩展的类。我的问题是如何从 Activity 调用 AsyncTaskLoader。我已经制作了 AsyncTaskLoader 类以及 WebService 类,如果输入的信息正确,它将带来用户的状态。但是我无法从我的 Activity 类中调用 AsyncTaskLoader。
public class LoginActivityService extends Activity
{ Context context;
EditText userName,password,version;
Button loginBtn,logoutBtn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login_view_service);
context = this;
userName = (EditText)findViewById(R.id.userName1);
password = (EditText)findViewById(R.id.password1);
loginBtn = (Button)findViewById(R.id.login1);
logoutBtn = (Button)findViewById(R.id.logout1);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(userName.getText().length() == 0 || password.getText().length() == 0)
{
Toast.makeText(context,"UserName Or Password Should be Filled",Toast.LENGTH_SHORT).show();
}
else
{
userName.setText("");
password.setText("");
//getLoaderManager().initLoader(101, null, this);
}
}
});
}
加载器类:
public class LoginLoader extends AsyncTaskLoader<List<User>>
{
private List<User> listUser;
Context context;
private final String TAG = User.class.getName();
public LoginLoader(Context context)
{super(context);
}
@Override
protected void onStartLoading() {
Log.d(TAG, "starting proposals loader...");
long currentTime = System.currentTimeMillis();
forceLoad();
}
public void loadinBackground()
{
// List<User> locations= LoginListService. // in this line there is a problem Function is not accessible
}
}
登录列表服务功能:
public List<User> getLoginResult()
{
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, WorkflowRestService
.getInstance().getRequestHeaders());
ResponseEntity<UserListItemHolder> responseEntity = WorkflowRestService.getInstance().getRestTemplate()
.exchange(WorkflowApp.getServicesURL()+"user/logIn",HttpMethod.POST, requestEntity, UserListItemHolder.class);
Log.i("response Entity Login",""+responseEntity);
UserListItemHolder userListItemInstance = responseEntity.getBody();
Log.i("response Entity Body Location Function",""+responseEntity.getBody());
if("true".equals(userListItemInstance.getStatus()))
{
Log.i("locationInstance.getLocationListItems if",""+userListItemInstance.getUserListItems());
return userListItemInstance.getUserListItems();
}
else
{
Log.i("locationInstance.getLocationListItems else",""+userListItemInstance.getUserListItems());
userListItemInstance = null;
return userListItemInstance.getUserListItems();
}
}