请帮助我,因为我是android的初学者。在此我可以使用相同的用户名和电子邮件ID注册多次,以便如何获得唯一的用户名和电子邮件ID。 还有如何将单选按钮值(男性或女性)添加到数据库中。我可以从微调器中选择多个值并显示到数据库中..提前感谢。
这是Register.java文件。
package com.example.login;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Register extends Activity implements OnItemSelectedListener {
EditText fname, lname, emailid, username, pass, cpass;
RadioGroup selection;
RadioButton male, female;
Spinner usrtyp, hobbies;
TextView type, hoby;
Button submit;
ContentValues values;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
db = openOrCreateDatabase("login", MODE_PRIVATE, null);
db.execSQL(
"CREATE TABLE IF NOT EXISTS USERS (ID integer PRIMARY KEY AUTOINCREMENT, F_NAME VARCHAR(200), L_NAME VARCHAR(200), EMAIL_ID VARCHAR(20), GENDER VARCHAR(20), USERNAME VARCHAR(200), PASSWORD VARCHAR(16), C_PASSWORD VARCHAR(16), HOBBIES VARCHAR(200), USER_TYPE VARCHAR(200));");
values = new ContentValues();
fname = (EditText) findViewById(R.id.firstname);
lname = (EditText) findViewById(R.id.lastname);
emailid = (EditText) findViewById(R.id.emailid);
selection = (RadioGroup) findViewById(R.id.radioGroup1);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
username = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
cpass = (EditText) findViewById(R.id.cpassword);
hoby = (TextView) findViewById(R.id.tvhobbies);
hobbies = (Spinner) findViewById(R.id.spinner2);
type = (TextView) findViewById(R.id.usertype);
usrtyp = (Spinner) findViewById(R.id.spinner1);
submit = (Button) findViewById(R.id.submit);
// Hobbies
hobbies.setOnItemSelectedListener(this);
List<String> hoby = new ArrayList<String>();
hoby.add("Sports");
hoby.add("Movies");
hoby.add("Travelling");
hoby.add("Reading");
ArrayAdapter<String> dataadapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
hoby);
dataadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
hobbies.setAdapter(dataadapter);
// User Type
usrtyp.setOnItemSelectedListener(this);
List<String> user = new ArrayList<String>();
user.add("Admin");
user.add("Normal User");
ArrayAdapter<String> dataadapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
user);
dataadapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
usrtyp.setAdapter(dataadapter1);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
String firstName = fname.getText().toString();
String lastName = lname.getText().toString();
String Email_ID = emailid.getText().toString();
String Gender = male.getText().toString();
String userName = username.getText().toString();
String Password = pass.getText().toString();
String confirmPassword = cpass.getText().toString();
String Hobbies = hobbies.getSelectedItem().toString();
String userType = usrtyp.getSelectedItem().toString();
//Check vacant
if (firstName.equals("") || lastName.equals("") || Email_ID.equals("") || userName.equals("")
|| Password.equals("") || confirmPassword.equals("")) {
Toast.makeText(getApplicationContext(), "Fill Vacant", Toast.LENGTH_LONG).show();
return;
}
//check if password doesn't match
if (!Password.equals(confirmPassword)) {
Toast.makeText(getApplicationContext(), "Password doesn't match", Toast.LENGTH_LONG).show();
return;
}
else
values.put("F_NAME", firstName);
values.put("L_NAME", lastName);
values.put("EMAIL_ID", Email_ID);
values.put("USERNAME", userName);
values.put("PASSWORD", Password);
values.put("GENDER", Gender);
values.put("C_PASSWORD", confirmPassword);
values.put("HOBBIES", Hobbies);
values.put("USER_TYPE", userType);
db.insert("USERS", null, values);
} catch (Exception e) {
System.out.println(e);
}
Toast.makeText(getApplicationContext(), "Successfully submited", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Login.java文件..
package com.example.login;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener {
EditText uname, pass;
Button login;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.signin);
uname = (EditText) findViewById(R.id.loginusername);
pass = (EditText) findViewById(R.id.loginpassword);
login = (Button) findViewById(R.id.login);
db = openOrCreateDatabase("login", MODE_PRIVATE, null);
login.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
String user_Name = uname.getText().toString();
String Password1 = pass.getText().toString();
// If username or password is not entered
if (user_Name.equals("") || Password1.equals("")) {
Toast.makeText(getApplicationContext(), "Please enter Username and Password", Toast.LENGTH_LONG).show();
return;
} else {
String query = "SELECT * FROM USERS WHERE userName = '" + user_Name + "' AND Password = '" + Password1
+ "'";
Cursor c = db.rawQuery(query, null);
if (c.moveToFirst()) {
Intent i = new Intent(Login.this, Home.class);
i.putExtra("un", user_Name);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Wrong Username or Password", Toast.LENGTH_LONG).show();
return;
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Home.java文件。
package com.example.login;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Home extends Activity {
TextView tv1;
ListView lv;
String un;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
lv = (ListView) findViewById(R.id.listView);
tv1 = (TextView) findViewById(R.id.textView1);
db = openOrCreateDatabase("login", MODE_PRIVATE, null);
Intent ii = getIntent();
Bundle b = ii.getExtras();
String temp = (String) b.get("un");
tv1.setText("Welcome " + temp);
int c=0;
try {
String query1 = "SELECT * FROM USERS;";
Cursor c1 = db.rawQuery(query1, null);
if(c1.moveToFirst()){
String[] temp2 = new String[c1.getCount()];
do{
String tempo =c1.getString(0) + " " + c1.getString(1) + " " + c1.getString(2) + " " + c1.getString(4) + " " + c1.getString(8) + " " + c1.getString(9);
temp2[c]=tempo;
c++;
//Toast.makeText(getApplicationContext(), c1.getString(1), Toast.LENGTH_LONG).show();
}
while(c1.moveToNext());
ArrayAdapter<String> adp = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1, temp2);
lv.setAdapter(adp);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
答案 0 :(得分:0)
SQLite提供的表中已有唯一的ID,请尝试:
select rowid,* from USERS
第一列将是自动生成的ID。
答案 1 :(得分:0)
"CREATE TABLE IF NOT EXISTS USERS (ID integer PRIMARY KEY AUTOINCREMENT,
F_NAME VARCHAR(200),
L_NAME VARCHAR(200),
EMAIL_ID VARCHAR(20) UNIQUE,
GENDER VARCHAR(20),
USERNAME VARCHAR(200) UNIQUE,
PASSWORD VARCHAR(16),
C_PASSWORD VARCHAR(16),
HOBBIES VARCHAR(200),
USER_TYPE VARCHAR(200));");
现在你不能重复用户名和电子邮件。 我建议在性别列使用1或0时使用char。 我不知道你的意思是说显示到数据库
答案 2 :(得分:0)
通过写这个,我得到了唯一的电子邮件ID:
String id = "SELECT * FROM USERS WHERE Email_ID = '" + Email_ID + "'";
Cursor c2 = db.rawQuery(id, null);
if (c2.getCount() > 0) {
Toast.makeText(getApplicationContext(), "Email ID already exists", Toast.LENGTH_LONG).show();
return;
}
用户名相同。