Reputation: 426
I have an aplication that gets the count of the user_id on the database to check if the user exists or not.
the code I am having problem:
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usernameR = usernameRgt.getText().toString();
emailR = emailRgt.getText().toString();
passwordR = passwordRgt.getText().toString();
repeatR = repeatPassRgt.getText().toString();
new userExistsTask().execute(new ApiConnector());
if ((usernameR == null || usernameR.isEmpty() == true) || (emailR == null || emailR.isEmpty() == true) || (passwordR == null || passwordR.isEmpty() == true) || (repeatR == null || repeatR.isEmpty() == true)) {
Toast.makeText(RegisterActivity.this, "One or more fields are empty!", Toast.LENGTH_SHORT).show();
} else {
if (userExistN == 0) {
Toast.makeText(RegisterActivity.this, "Não existe", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "existe", Toast.LENGTH_SHORT).show();
}
}
}
});
It works the way I want to the problem is that the call to the new userExistsTask().execute(new ApiConnector());
(method that set the variable userExistsN
value) takes some time to execute and the if gets the wrong value to the variable userExistsN
so there is a way to put a delay between the if and the method call?
Update:
userExistYask() code:
private class userExistsTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
return params[0].userExists(usernameR);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
if (jsonArray != null) {
JSONObject json = null;
try {
json = jsonArray.getJSONObject(0);
userExistN = json.getInt("userCount");
System.out.println(userExistN);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(RegisterActivity.this, "Null", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Views: 47
Reputation: 2737
Create an Interface
public interface AsyncResponse {
void processFinish(int userExistN);
}
In userExistsTask class:
private class userExistsTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
public AsyncResponse delegate=null;
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].userExists(usernameR);
}
:
:
:
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
delegate.processFinish(userExistN);
}
In Activity onCreate()
userExistsTask existsTask = new userExistsTask();
existsTask.execute(new ApiConnector());
existsTask.delegate = this;
void processFinish(int userExistN){
if ((usernameR == null || usernameR.isEmpty() == true) || (emailR == null || emailR.isEmpty() == true) || (passwordR == null || passwordR.isEmpty() == true) || (repeatR == null || repeatR.isEmpty() == true)) {
Toast.makeText(RegisterActivity.this, "One or more fields are empty!", Toast.LENGTH_SHORT).show();
} else {
if (userExistN == 0) {
Toast.makeText(RegisterActivity.this, "Não existe", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "existe", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Reputation: 43322
There's a couple issues here. You're calling your AsyncTask even if the text fields are empty, which is not needed.
To solve your main issue, just move the functionality that requires the result of the AsyncTask to onPostExecute().
Something like this:
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usernameR = usernameRgt.getText().toString();
emailR = emailRgt.getText().toString();
passwordR = passwordRgt.getText().toString();
repeatR = repeatPassRgt.getText().toString();
if ((usernameR == null || usernameR.isEmpty() == true) || (emailR == null || emailR.isEmpty() == true) || (passwordR == null || passwordR.isEmpty() == true) || (repeatR == null || repeatR.isEmpty() == true)) {
Toast.makeText(RegisterActivity.this, "One or more fields are empty!", Toast.LENGTH_SHORT).show();
} else {
new userExistsTask().execute(new ApiConnector());
}
}
});
Then your AsyncTask:
private class userExistsTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
return params[0].userExists(usernameR);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
if (jsonArray != null) {
JSONObject json = null;
try {
json = jsonArray.getJSONObject(0);
userExistN = json.getInt("userCount");
//change this to Lod.d(Tag, userExistN);
System.out.println(userExistN);
if (userExistN == 0) {
Toast.makeText(getApplicationContext(), "Não existe", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "existe", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(RegisterActivity.this, "Null", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 1
Reputation: 14678
Why don't you put the IF code in separate method and call this method from postExecute? This guarantees the method won't be called till the background method is done
Upvotes: 3
Reputation: 4766
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
}
}, 5000);
Upvotes: 1