Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:11:59+00:00 2026-05-31T01:11:59+00:00

I’m writing an activity in Android where the user modifies an SQL Database. The

  • 0

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.

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.

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.

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.

Once the input has been entered, the thread is notified and the rest of the edit function continues. (The code is below).

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?

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.

Thanks for your help.

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(); 
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:12:00+00:00Added an answer on May 31, 2026 at 1:12 am

    No, no, no! Do not block the UI thread. The system will raise an “Application Not Responding” error. Also, do not try to interact with the user from a non-UI thread.

    When the user clicks “edit”, don’t start the edit method. Just pop up a dialog to collect the required information. Add a DialogInterface.OnClickListener to the positive button and (with the required information now in hand) start the edit method from there.

    See the guide topic Dialogs for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am writing an app with both english and french support. The app requests
I have a reasonable size flat file database of text documents mostly saved in
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.