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

  • Home
  • SEARCH
  • 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 8307679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:34:38+00:00 2026-06-08T18:34:38+00:00

It’s me again, David. I’m just wondering if my code is in correction. I’m

  • 0

It’s me again, David. I’m just wondering if my code is in correction. I’m trying to figure out if the database (Database_Handler.java) can check on an EditText named label_input. If it’s existed already on the database, the registration is canceled. but, my problem is that, even if have no bugs, the condition ignores and automatically jumps to the else statement. hope you can help me out and thanks in advance.

Here’s my code (Database_Handler.java):

//Variables
private Button add_button, proceed_to_player_spinner_menu;
private EditText label_input;
//private SQLiteDatabase datab;

//Response
public final static String EXTRA_MESSAGE_1 = "com.example.databasetestvertwo.MESSAGE1";
public final static String EXTRA_MESSAGE_2 = "com.example.databasetestvertwo.MESSAGE2";
public final static String EXTRA_MESSAGE_3 = "com.example.databasetestvertwo.MESSAGE3";




@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.register_menu);

    //Setting for the players' list.
    add_button = (Button) findViewById(R.id.register_name);
    proceed_to_player_spinner_menu = (Button) findViewById(R.id.Proceed_Button_to_Spinner);
    label_input = (EditText) findViewById(R.id.Name_of_Player_Text_Box);

    //Function for Buttons to be add as a new player's name.
    add_button.setOnClickListener(new OnClickListener() 
    {

        public void onClick(View v) 
        {

            String label = label_input.getText().toString();

            //Here's the process on how to register in the database.
            if(label.trim().length() > 0)
            {
                //Database Handler from Class (Database_Handler.java)
                Database_Handler db = new Database_Handler(getApplicationContext());

                //Inserting new label into the database.
                //db.insertLabel(label);

                if(db.getAllLabels().toArray().toString().equals(label))
                {
                    Toast.makeText(getApplicationContext(), "Name existed.", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Name available!", Toast.LENGTH_SHORT).show();
                }

                //Message Confirmation
                //Toast.makeText(getApplicationContext(), "Player name " + label + " have been confirmed!", Toast.LENGTH_SHORT).show();

                //After typing, the text field is set to blank.
                //label_input.setText("");

                //Normally, most smartphones and tablets only have a virtual keyboard.
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(label_input.getWindowToken(), 0);

                //loadSpinnerData();
            }

            else //If the input is null...
            {
                Toast.makeText(getApplicationContext(), "Please enter your name.", Toast.LENGTH_SHORT).show();
            }

        }

    });

    proceed_to_player_spinner_menu.setOnClickListener(new OnClickListener() 
    {

        public void onClick(View v) 
        {

            startActivity(new Intent(from_3_players.this, Player_at_3_Spinner_Menu.class));

        }

    });

}

…And code for the Database_Handler.java

//Main Variables for the Database Handler
private static final int DATABASE_VERSION = 1; //Version of the Database
private static final String DATABASE_NAME = "spinnerExample"; //Name of the Database
public static final String TABLE_LABELS = "labels"; // Table Name

//Columns for the Database
public static final String KEY_ID = "id";
private static final String KEY_NAME = "name";





public Database_Handler(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}






@Override
public void onCreate(SQLiteDatabase db) //Used for creating a table. 
{

    //Create Table Query Category 
    /*
     * 
     * 
     *      WARNING! Under CREATE_CATEGORIES_TABLE, strictly type the exact words on
     *      all 4 values with quotations, even the space. Exception only is the name
     *      from the variable names under "private static final String" here.  
     * 
     * 
     */
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "(" + KEY_ID +
            " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";

    db.execSQL(CREATE_CATEGORIES_TABLE); //Executes the query.

}





@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) //Updates the info.
{

    //Drops the old existing table.
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

    //Creates table again.
    onCreate(db);

}





/*
 * 
 *             The next section will create a new label into the tables.
 *          This allows the database to harvest the identifier for each
 *          column. And, the database must set a list of person so that
 *          it applies in an infinite number of registration.  
 * 
 */





//Inserts a new label into the table.
public void insertLabel(String label)
{

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, label);

    //Inserting Row
    db.insert(TABLE_LABELS, null, values);
    db.close(); //Always close the database to prevent running dynamically. It has a pause to process one at a time.

}





//Returns all labels stored in the database.
public List<String> getAllLabels()
{

    List<String> labels = new ArrayList<String>(); //This must be return.

    //Select all Query it have.
    String selectQuery = "SELECT * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    //Do the loop by rows to add new in the database
    if(cursor.moveToFirst())
    {
        do
        {
            labels.add(cursor.getString(1));
        }

        while(cursor.moveToNext());
    }

    //Always close the connections.
    cursor.close();
    db.close();

    //Return the labels for feedback and response. the "return" keyword can be like a value set for process.
    return labels;

}
  • 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-06-08T18:34:39+00:00Added an answer on June 8, 2026 at 6:34 pm

    ur trying to compare array with ur label.

    db.getAllLabels().toArray().toString() here ur getting list of names from database as list(List), then ur converting this list as Array by using toArray().
    Then u converting this array as string using toString(), this will return string like java.lang.Object;.., so this is not any name which is in database.

    so have to write one more method to compare given name with the list of item which are retrieved from database. for ex.

    if (isNameExists(label)) {
         //show positive Toast  
    } else {
        //show negative Toast
    }
    

    // Method isNameExists() as follows

    private boolean isNameExists(String name) {
        Database_Handler db = new Database_Handler(getApplicationContext());
    
        List<String> nameList = db.getAllLabels();
    
        for(String nameInDB : nameList) {
             if (nameInDB.equals(name)
                 return true;
        }
    
        return false;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.