title: Android: Wait() the main thread while a dialog gets input in a separate Thread
body: <p>I'm writing an activity in Android where the user modifies an SQL Database. The UI consists of an EditText where the user enters the name, and a Seekbar where the user enters how attractive the person is. Underneath there are a bunch of buttons: add, edit, view, delete. </p>
  
  <p>When the user clicks on the "Edit" button, an input dialog is displayed asking the user to input the record number. Once that is done, that record is loaded. </p>
  
  <p>The problem I was having was that the inputdialog would be displayed and while the user entering the record no, the rest of the edit method would carry on so that by the time the user was done entering the input - nothing happened because the function had already been completed. </p>
  
  <p>In order to solve this problem, I decided to use Multi-Threads (which I do not have much experience using). When the edit button is pressed, the main UI Thread is blocked (using wait() - this is because I don't want the UI to be active while the user is entering the record id.) and the input dialog is displayed in a seperate thread. </p>
  
  <p>Once the input has been entered, the thread is notified and the rest of the edit function continues. (The code is below). </p>
  
  <p><strong>The problem is that when I call the wait function on the UI Thread, I get an error that says "object not locked by thread before wait() ". How do I lock the UI Thread?</strong></p>
  
  <p>I know in general one shouldn't block the UI Thread, but I think it's okay in this case because I don't want it to accept any user input. </p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>public class Attractivometer extends Activity implements OnClickListener {
  
      private Button      buttonAddRecord,    buttonEditRecord,   buttonSaveChanges;
      private Button      buttonDeleteRecord, buttonViewRecord;
      private EditText    fieldName;
      private SeekBar     seekbarAttractiveness;
      private String      inputFromInputDialog=null;
      private Thread      inputThread;
  
  
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.Attractivometer);
  
          buttonAddRecord         = (Button) findViewById(R.id.buttonAddRecord);
          buttonSaveChanges       = (Button) findViewById(R.id.buttonSaveChanges);
          buttonEditRecord        = (Button) findViewById(R.id.buttonEditRecord);
          buttonDeleteRecord      = (Button) findViewById(R.id.buttonDeleteRecord);
          buttonViewRecord        = (Button) findViewById(R.id.buttonViewRecord);
  
          fieldName               = (EditText) findViewById(R.id.fieldName);
  
          seekbarAttractiveness           = (SeekBar) findViewById(R.id.seekbarAttractiveness);
  
          buttonAddRecord.setOnClickListener(this);
          buttonSaveChanges.setOnClickListener(this);
          buttonEditRecord.setOnClickListener(this);
          buttonDeleteRecord.setOnClickListener(this);
          buttonViewRecord.setOnClickListener(this);
      }
  
  
      public void onClick(View clickedItem)
      {
  
          switch(clickedItem.getId())
          {
          case R.id.buttonAddRecord:
              //.....
              break;
  
  
          case R.id.buttonSaveChanges:
            //... 
              break;
  
          case R.id.buttonEditRecord:
  
              inputThread = new Thread(new Runnable(){
  
                  public void run()
                  {
                      showInputDialog("Enter Record ID", InputType.TYPE_CLASS_NUMBER);
                  }
  
              });
              inputThread.start();
  
  
              try {
                  Thread.currentThread().wait();
              } catch (InterruptedException e) {
                  Log.e("Attractivometer","Main Thread interrupted while waiting");
                  e.printStackTrace();
              }
  
              try {
                  inputThread.join();
              } catch (InterruptedException e) {
                  Log.e("Attractivometer","Input Thread interrupted while joining");
                  e.printStackTrace();
              }
  
              int recordId = Integer.parseInt(inputFromInputDialog);
              if(recordId!=null)
              {
                  AttractivometerSQLHandler AttractivometerDatabaseHandler = new AttractivometerSQLHandler(this);
                  AttractivometerDatabaseHandler.openDatabase();
                  String recordName = AttractivometerDatabaseHandler.getName(recordId);
                  String recordAttractiveness = AttractivometerDatabaseHandler.getAttractiveness(recordId);
  
                  if(recordName==null || recordAttractiveness==null )
                  {
                      //no record found.
                      Toast.makeText(this, "No record with that ID found", Toast.LENGTH_SHORT).show();
                  }else
                  {
                      fieldName.setText(recordName);
                      seekbarAttractiveness.setProgress( Integer.parseInt(recordAttractiveness) );
                      recordIsOpen(true);
                  }
  
  
                  AttractivometerDatabaseHandler.closeDatabase();
              }else
                  //No input.
              recordIsOpen(false);
  
              break;
  
          case R.id.buttonDeleteRecord:
              //...
              break;
  
          case R.id.buttonViewRecord:
              //....
  
          }
      }
  
      private void showInputDialog(String prompt, int inputType)
      {
          AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);
  
          inputDialog.setTitle("Record No.");
  
          final EditText fieldInput = new EditText(this);
          fieldInput.setInputType(inputType);
          fieldInput.setHint(prompt);
  
          inputDialog.setView(fieldInput);
          inputDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
          {
  
              public void onClick(DialogInterface arg0, int arg1)
              {
                  inputFromInputDialog = fieldInput.getText().toString();
                  inputThread.notify();
  
              }
          });
  
          inputDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
          {
  
              public void onClick(DialogInterface arg0, int arg1)
              {
                  inputFromInputDialog = null;
                  inputThread.notify();
              }
          });
  
          inputDialog.show(); 
      }   
  }
  </code></pre>