【发布时间】:2016-06-02 05:31:07
【问题描述】:
我可以在 setOnClickListener 之外写postAsync = new PostAsync();
postAsync.delegate = this;,这样可以顺利运行,但我需要在 setOnClickListener 中写。
public class Sign_inFragment extends Fragment implements AsyncResponse {
PostAsync postAsync;
String email, password, logInResult;
EditText ev, pv;
Button bv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.sign_in_fragment, container, false);
bv = (Button) v.findViewById(R.id.signinButton);
ev = (EditText) v.findViewById(R.id.emailTextView);
pv = (EditText) v.findViewById(R.id.passwordTextView);
bv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ev.getText() != null && pv.getText() != null) {
email = ev.getText().toString();
password = pv.getText().toString();
postAsync = new PostAsync();
postAsync.delegate = this;//this will not work.
postAsync.execute(email, password);
//Toast.makeText(getActivity().getApplicationContext(), "SIGN IN SUCCESFUL", Toast.LENGTH_LONG).show();
}
}
});
return v;
}
@Override
public void processFinish(String output) {
logInResult = output;
if (logInResult.equals("true") ) {
Intent intent = new Intent(getActivity().getApplicationContext(), SignedInActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else if(logInResult.equals("false")) {
ev.setError("Invalid Account");
}
}
}
显然写postAsync.delegate = this 是行不通的。你有什么建议吗?
【问题讨论】:
标签: android-fragments android-studio android-asynctask android-studio-2.0