我正在为我的应用创建一个注册页面。当用户键入他们的用户名,密码和电子邮件时,它将存储在我的数据库中。我也有性别的微调器。如何在数据库中存储他们为性别选择的内容?我对android编程比较陌生,所以对我很轻松:(
我的SignUp.Java
package com.nyp.integrateshoebox;
import java.util.regex.Pattern;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class SignUp extends Activity {
TextView textView1, textView2, textView3, textView4, textView5, textView6;
EditText username, password, repassword, emailAddress;
Button signUp;
Spinner spinner1;
final Context context = this;
LoginDataBaseAdapter loginDataBaseAdapter;
public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
repassword = (EditText)findViewById(R.id.editText3);
emailAddress = (EditText)findViewById(R.id.editText4);
signUp = (Button)findViewById(R.id.button1);
signUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=username.getText().toString();
String Password=password.getText().toString();
String confirmPassword=repassword.getText().toString();
String email=emailAddress.getText().toString();
// check if any of the fields are vacant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")||email.equals(""))
{
Toast.makeText(getApplicationContext(), "Please fill up empty fields!", Toast.LENGTH_LONG).show();
return;
}
if( username.getText().toString().length() <= 6 )
{
Toast.makeText(getApplicationContext(), "Username has to be more than 6 character long!", Toast.LENGTH_LONG).show();
return;
}
else if( password.getText().toString().length() < 5 )
{
Toast.makeText(getApplicationContext(), "Password has to be more than 5 characters long!", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!Password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password does not match!", Toast.LENGTH_LONG).show();
return;
}
else if( checkEmail(email) == false )
{
Toast.makeText(getApplicationContext(), "Invalid Email Address!", Toast.LENGTH_LONG).show();
return;
}
else
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Confirm?");
// set dialog message
alertDialogBuilder
.setMessage("Are you sure the information is correct?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(getApplicationContext(), "Account Created. Welcome to Shoebox!",
Toast.LENGTH_SHORT).show();
Intent home = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(home);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, Password,email);
}
});
Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
String[] items = new String[]{"Male", "Female"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
dropdown.setAdapter(adapter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginDataBaseAdapter.close();
}
private boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_up, menu);
return true;
}
}
这是我的LoginDataBaseAdpater.Java
package com.nyp.integrateshoebox;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text, EMAIL text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password, String email)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("EMAIL", email);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password, String email)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
updatedValues.put("EMAIL",email);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
答案 0 :(得分:0)
您需要在表中为性别添加新字段,并将值从微调器传递到数据库。
1)将字段添加到表中:
static final String DATABASE_CREATE = "create table "+"LOGIN"+ "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text, EMAIL text, GENDER Integer); ";
2)更新插入方法:
public void insertEntry(String userName,String password, String email, int gender)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("EMAIL", email);
newValues.put("GENDER", gender);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
3)从微调器获取值并将其传递给insert方法: (使用ISO 5218)
Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
String strgender = (String) dropdown.getSelectedItem();
int gender = 0;
switch(strgender){
case "Male":
gender = 1;
break;
case "Female":
gender = 2;
break;
}
loginDataBaseAdapter.insertEntry(userName, Password,email, gender);
请注意,您也应该将新字段添加到updateEntry()方法中。