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;
}
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.
// Method isNameExists() as follows