In my app when a button clicked, the database is updated first, then that data is sent to the server. Before sending data that data is encrypted. The update to the db is done in one async task and the encryption and sending data is done through another async task like the below
1) update db
private class UpdateThread extends AsyncTask<Void, Void, Void> {
private MessageThread messageThread;
private UpdateThread(MessageThread thread) {
this.messageThread = thread;
}
@Override
protected Void doInBackground(Void... params) {
action.update(messageThread);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
PollReply pollReply = new PollReply(messageThread);
pollReply.execute();
}
}
2) encrypt data and send to server
private class PollReply extends AsyncTask<Void, Void, Void> {
private MessageThread messageThread;
private PollReply(MessageThread thread) {
this.messageThread = thread;
}
@Override
protected Void doInBackground(Void... params) {
messageThread.setHashcode(HashUtility.encryptString(messageThread.getRandomcode() + messageThread.getUserId()));
messageThread.setStrippedHashCode(StringUtility.reduceToBytes(messageThread.getHashcode(), 16));
try {
messageThread.setAnswerEnc(EncryptionUtility.encrypt(messageThread.getAnswer(), messageThread.getStrippedHashCode()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
PollReplyAsyncTask asyncTask = new PollReplyAsyncTask(activity, false, messageThread, messageThread.getUserId());
asyncTask.execute();
}
}
and these async tasks are called like below
UpdateThread updateThread = new UpdateThread(message);
updateThread.execute();
PollReply pollReply = new PollReply(message);
pollReply.execute();
When I comment the second async task, the db is updated immediately and UI is updated immediately too. But when I use the second async task, the db is updated but the ui is updated after the execution of second async task executed. I think UI thread is blocked somewhere. Any solution to this problem?
if (message.getOptionsCount() > 0) {
if (message.getOptionsCount() > 1) {
pollChoice1.setText(message.getOption1());
pollChoice2.setText(message.getOption2());
backgroundWidth = 0;
if (message.getOptionsCount() == 2) {
pollChoice1.setVisibility(View.VISIBLE);
pollChoice2.setVisibility(View.VISIBLE);
pollChoice3.setVisibility(View.GONE);
pollChoice4.setVisibility(View.GONE);
pollChoice5.setVisibility(View.GONE);
if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
pollChoice3Background.setVisibility(View.GONE);
pollChoice4Background.setVisibility(View.GONE);
pollChoice5Background.setVisibility(View.GONE);
if (responseCount > 0) {
pollChoice1Background.setVisibility(View.VISIBLE);
pollChoice2Background.setVisibility(View.VISIBLE);
float widthFloat = (float) (option1Count * 100.0 / responseCount);
backgroundWidth = (int) widthFloat;
int total_width = pollChoice1.getMeasuredWidth();
pollChoice1Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice1Background.getLayoutParams().width = backgroundWidth;
widthFloat = (float) (option2Count * 100.0 / responseCount);
backgroundWidth = (int) widthFloat;
total_width = pollChoice1.getMeasuredWidth();
pollChoice2Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice2Background.getLayoutParams().width = backgroundWidth;
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
}
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
}
line1choice.setVisibility(View.VISIBLE);
line2choice.setVisibility(View.VISIBLE);
line3choice.setVisibility(View.VISIBLE);
choice1Layout.setVisibility(View.VISIBLE);
choice2Layout.setVisibility(View.VISIBLE);
choice3Layout.setVisibility(View.GONE);
choice4Layout.setVisibility(View.GONE);
choice5Layout.setVisibility(View.GONE);
choice6Layout.setVisibility(View.GONE);
} else if (message.getOptionsCount() == 3) {
dps = 150;
pixels = (int) (dps * scale + 0.5f);
feed.getLayoutParams().height = pixels;
pollChoice1.setText(message.getOption1());
pollChoice2.setText(message.getOption2());
pollChoice3.setText(message.getOption3());
pollChoice1.setVisibility(View.VISIBLE);
pollChoice2.setVisibility(View.VISIBLE);
pollChoice3.setVisibility(View.VISIBLE);
pollChoice4.setVisibility(View.GONE);
pollChoice5.setVisibility(View.GONE);
if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
pollChoice4Background.setVisibility(View.GONE);
pollChoice5Background.setVisibility(View.GONE);
if (responseCount > 0) {
pollChoice1Background.setVisibility(View.VISIBLE);
pollChoice2Background.setVisibility(View.VISIBLE);
pollChoice3Background.setVisibility(View.VISIBLE);
float width_float = (float) (option1Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
int total_width = pollChoice1.getMeasuredWidth();
pollChoice1Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice1Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option2Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice2.getMeasuredWidth();
pollChoice2Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice2Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option3Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice3.getMeasuredWidth();
pollChoice3Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice3Background.getLayoutParams().width = backgroundWidth;
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
pollChoice3Background.setVisibility(View.INVISIBLE);
}
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
pollChoice3Background.setVisibility(View.INVISIBLE);
}
line1choice.setVisibility(View.VISIBLE);
line2choice.setVisibility(View.VISIBLE);
line3choice.setVisibility(View.VISIBLE);
line4choice.setVisibility(View.VISIBLE);
choice1Layout.setVisibility(View.VISIBLE);
choice2Layout.setVisibility(View.VISIBLE);
choice3Layout.setVisibility(View.VISIBLE);
choice4Layout.setVisibility(View.GONE);
choice5Layout.setVisibility(View.GONE);
choice6Layout.setVisibility(View.GONE);
} else if (message.getOptionsCount() == 4) {
dps = 100;
pixels = (int) (dps * scale + 0.5f);
feed.getLayoutParams().height = pixels;
pollChoice1.setText(message.getOption1());
pollChoice2.setText(message.getOption2());
pollChoice3.setText(message.getOption3());
pollChoice4.setText(message.getOption4());
pollChoice1.setVisibility(View.VISIBLE);
pollChoice2.setVisibility(View.VISIBLE);
pollChoice3.setVisibility(View.VISIBLE);
pollChoice4.setVisibility(View.VISIBLE);
pollChoice5.setVisibility(View.GONE);
if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
pollChoice5Background.setVisibility(View.GONE);
if (responseCount > 0) {
pollChoice1Background.setVisibility(View.VISIBLE);
pollChoice2Background.setVisibility(View.VISIBLE);
pollChoice3Background.setVisibility(View.VISIBLE);
pollChoice4Background.setVisibility(View.VISIBLE);
float width_float = (float) (option1Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
int total_width = pollChoice1.getMeasuredWidth();
pollChoice1Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice1Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option2Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice2.getMeasuredWidth();
pollChoice2Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice2Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option3Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice3.getMeasuredWidth();
pollChoice3Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice3Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option4Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice4.getMeasuredWidth();
pollChoice4Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice4Background.getLayoutParams().width = backgroundWidth;
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
pollChoice3Background.setVisibility(View.INVISIBLE);
pollChoice4Background.setVisibility(View.INVISIBLE);
}
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
pollChoice3Background.setVisibility(View.INVISIBLE);
pollChoice4Background.setVisibility(View.INVISIBLE);
}
line1choice.setVisibility(View.VISIBLE);
line2choice.setVisibility(View.VISIBLE);
line3choice.setVisibility(View.VISIBLE);
line4choice.setVisibility(View.VISIBLE);
line5choice.setVisibility(View.VISIBLE);
choice1Layout.setVisibility(View.VISIBLE);
choice2Layout.setVisibility(View.VISIBLE);
choice3Layout.setVisibility(View.VISIBLE);
choice4Layout.setVisibility(View.VISIBLE);
choice5Layout.setVisibility(View.GONE);
choice6Layout.setVisibility(View.GONE);
} else if (message.getOptionsCount() == 5) {
dps = 100;
pixels = (int) (dps * scale + 0.5f);
feed.getLayoutParams().height = pixels;
pollChoice1.setText(message.getOption1());
pollChoice2.setText(message.getOption2());
pollChoice3.setText(message.getOption3());
pollChoice4.setText(message.getOption4());
pollChoice5.setText(message.getOption5());
pollChoice1.setVisibility(View.VISIBLE);
pollChoice2.setVisibility(View.VISIBLE);
pollChoice3.setVisibility(View.VISIBLE);
pollChoice4.setVisibility(View.VISIBLE);
pollChoice5.setVisibility(View.VISIBLE);
if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
if (responseCount > 0) {
pollChoice1Background.setVisibility(View.VISIBLE);
pollChoice2Background.setVisibility(View.VISIBLE);
pollChoice3Background.setVisibility(View.VISIBLE);
pollChoice4Background.setVisibility(View.VISIBLE);
pollChoice5Background.setVisibility(View.VISIBLE);
float width_float = (float) (option1Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
int total_width = pollChoice1.getMeasuredWidth();
pollChoice1Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice1Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option2Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice2.getMeasuredWidth();
pollChoice2Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice2Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option3Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice3.getMeasuredWidth();
pollChoice3Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice3Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option4Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice4.getMeasuredWidth();
pollChoice4Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice4Background.getLayoutParams().width = backgroundWidth;
width_float = (float) (option5Count * 100.0 / responseCount);
backgroundWidth = (int) width_float;
total_width = pollChoice5.getMeasuredWidth();
pollChoice5Background.setText("" + backgroundWidth + "%");
backgroundWidth = (total_width * backgroundWidth / 100);
pollChoice5Background.getLayoutParams().width = backgroundWidth;
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
pollChoice3Background.setVisibility(View.INVISIBLE);
pollChoice4Background.setVisibility(View.INVISIBLE);
pollChoice5Background.setVisibility(View.INVISIBLE);
}
} else {
pollChoice1Background.setVisibility(View.INVISIBLE);
pollChoice2Background.setVisibility(View.INVISIBLE);
pollChoice3Background.setVisibility(View.INVISIBLE);
pollChoice4Background.setVisibility(View.INVISIBLE);
pollChoice5Background.setVisibility(View.INVISIBLE);
}
line1choice.setVisibility(View.VISIBLE);
line2choice.setVisibility(View.VISIBLE);
line3choice.setVisibility(View.VISIBLE);
line4choice.setVisibility(View.VISIBLE);
line5choice.setVisibility(View.VISIBLE);
line6choice.setVisibility(View.VISIBLE);
choice1Layout.setVisibility(View.VISIBLE);
choice2Layout.setVisibility(View.VISIBLE);
choice3Layout.setVisibility(View.VISIBLE);
choice4Layout.setVisibility(View.VISIBLE);
choice5Layout.setVisibility(View.VISIBLE);
choice6Layout.setVisibility(View.GONE);
}
}
}
the above code is where i update the Ui. Here db gets updated immediately. But the Ui is not updated accordingly. I update the Ui in bindView() of the custom adapter.