又是我,大卫。我只是想知道我的代码是否正确。我试图弄清楚数据库(Database_Handler.java)是否可以检查名为label_input的EditText。如果它已经存在于数据库中,则取消注册。但是,我的问题是,即使没有错误,条件也会忽略并自动跳转到else语句。希望你能帮助我,并提前感谢。
这是我的代码(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));
}
});
}
...以及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;
}