I have an onClickListener that starts a network call, so I would like to have a way to show the user that the connection is in progress. The problem I am facing is that I cannot apparently throw away the ProgressDialog or change the user interface in any way before the call is made inside the onClick listener. All code works very well, but user interface changes do not take effect until all the code in onClickListener has been run.
I was wondering if my problem is simply that an anonymous inner class, such as onclicklistener, can only update the user interface at the end of its launch? Or maybe my code is just bad.
Thanks in advance
Below is the code for the onclick listener:
relayButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cPanel.throwProgress(NCDTCPRelayActivity.this); System.out.println(tvSocketConnection.getText().toString()); if (relayStatusArray[relayNumber] == 0) { if (cPanel.TurnOnRelay(relayNumber, 1) == false) { changeTitleToRed(); }else{ changeTitleToGreen(); } } else { if (cPanel.TurnOffRelay(relayNumber, 1) == false){ changeTitleToRed(); }else{ changeTitleToGreen(); } } cPanel.hideProgress(NCDTCPRelayActivity.this); } });
Here is the code for throwProgress and hideProgress respectively (they are in the activity subclass):
public boolean throwProgress(Context mContext) { System.out.println("INSIDE THROWPROGRESS"); try { tempDialog = ProgressDialog.show(mContext, "Connecting", "Connecting", true); } catch (RuntimeException e) { return false; } return true; } public boolean hideProgress(Context mContext) { System.out.println("OUTSIDE THROWPROGRESS"); tempDialog.hide(); return true; }
** Edit Here is the new code for onClickListener, which I entered runnable in:
public void onClick(View v) { cPanel.throwProgress(NCDTCPRelayActivity.this); System.out.println(tvSocketConnection.getText().toString()); handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if (relayStatusArray[relayNumber] == 0) { if (cPanel.TurnOnRelay(relayNumber, 1) == false) { changeTitleToRed(); }else{ changeTitleToGreen(); } } else { if (cPanel.TurnOffRelay(relayNumber, 1) == false){ changeTitleToRed(); }else{ changeTitleToGreen(); } } cPanel.hideProgress(NCDTCPRelayActivity.this); relayStatusArray = cPanel.getBankStatus(1); updateButtonText(); } }); }
source share