DBHandle
public class DBHandler extends SQLiteOpenHelper {
public DBHandler(Context context) {
super(context, UserProfile.Users.DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String createTable = "CREATE TABLE "+UserProfile.Users.TABLE_NAME+"("+ UserProfile.Users.COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT,"+UserProfile.Users.COL_2+" TEXT, "+ UserProfile.Users.COL_3 +" TEXT, "+ UserProfile.Users.COL_4 +" TEXT, "+ UserProfile.Users.COL_5+" TEXT "+")";
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + UserProfile.Users.TABLE_NAME);
}
public boolean addInfo(UserProfile.Users users){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(UserProfile.Users.COL_2,users.getUserName());
cv.put(UserProfile.Users.COL_3,users.getDateOfBirth());
cv.put(UserProfile.Users.COL_4,users.getPassword());
cv.put(UserProfile.Users.COL_5,users.getGender());
long result = db.insert(UserProfile.Users.TABLE_NAME,null,cv);
if(result == -1){
return false;
}
else
return true;
}
public boolean updateData(UserProfile.Users users){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
//cv.put(UserProfile.Users.COL_1,users.getID());
cv.put(UserProfile.Users.COL_2,users.getUserName());
cv.put(UserProfile.Users.COL_3,users.getDateOfBirth());
cv.put(UserProfile.Users.COL_4,users.getPassword());
cv.put(UserProfile.Users.COL_5,users.getGender());
db.update(UserProfile.Users.TABLE_NAME,cv,"username= ?",new String[]{users.getUserName()});
return true;
}
public Cursor readAllInfor(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+UserProfile.Users.TABLE_NAME,null);
return res;
}
public int deleteData(UserProfile.Users user){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(UserProfile.Users.TABLE_NAME,"username= ?",new String[]{user.getUserName()});
}
}
个人资料
public class ProfileManagement extends AppCompatActivity {
DBHandler mydb;
EditText username1,password1,dob;
Button save;
RadioGroup radioGroup1;
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_management);
mydb = new DBHandler(this);
username1 = (EditText)findViewById(R.id.username);
password1 = (EditText)findViewById(R.id.password);
dob = (EditText)findViewById(R.id.dateOfBirth);
save = (Button)findViewById(R.id.update);
radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup);
addData();
}
public void addData(){
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selectedID = radioGroup1.getCheckedRadioButtonId();
if (selectedID == 0) {
Toast.makeText(ProfileManagement.this, "cannot be empty", Toast.LENGTH_SHORT).show();
} else {
radioButton = (RadioButton) findViewById(selectedID);
String username = username1.getText().toString();
String password = password1.getText().toString();
String dateOfBirth = dob.getText().toString();
String radio = radioButton.getText().toString();
UserProfile.Users users = UserProfile.getProfile().getUsers();
users.setUsername(username);
users.setDob(dateOfBirth);
users.setPassword(password);
users.setGender(radio);
boolean isInserted = mydb.addInfo(users);
if (isInserted == true) {
Toast.makeText(ProfileManagement.this, "Data Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ProfileManagement.this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
用户
package com.google.hangout.it17092548_mad;
import android.provider.BaseColumns;
public class UserProfile {
private UserProfile(){
}
public static UserProfile getProfile(){
UserProfile userProfile = new UserProfile();
return userProfile;
}
public class Users implements BaseColumn{
public static final String DATABASE_NAME = "database.db";
public static final String TABLE_NAME = "user_table";
public static final String COL_1 ="ID";
public static final String COL_2="username";
public static final String COL_3 = "dateofbirth";
public static final String COL_4 = "password";
public static final String COL_5 = "gender";
private int ID;
private String userName;
private String dateOfBirth;
private String password;
private String gender;
public String getPassword() {
return password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setPassword(String password) {
this.password = password;
}
public void setID(int ID) {
this.ID = ID;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getID() {
return ID;
}
public String getUserName() {
return userName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
}
public Users getUser(){
Users users = new Users();
return users;
}
}
家
public class Home extends AppCompatActivity {
DBHandler mydb;
Button reg,log;
EditText username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mydb = new DBHandler(this);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
reg = findViewById(R.id.register);
log = findViewById(R.id.login);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent t1 = new Intent(Home.this,ProfileManagement.class);
startActivity(t1);
}
});
log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* if(username.equals("") || username == null){
Toast.makeText(getApplicationContext(), "Username Empty",
Toast.LENGTH_SHORT).show();
}
else if(password.equals("") || password == null){
Toast.makeText(getApplicationContext(), "Password Empty",
Toast.LENGTH_SHORT).show();
}
Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[] {username, password});
if(c.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
Intent main = new Intent(Home.this, EditProfile.class);
startActivity(main);
}
else {
Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
}*/
}
});
}
}
编辑
public class EditProfile extends Activity {
DBHandler myDb;
Button viewDetails;
Button dele,update;
EditText username1,DofB,password;
RadioGroup radioGroup;
RadioButton genderRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
myDb = new DBHandler(this);
viewDetails = (Button)findViewById(R.id.search);
dele = (Button)findViewById(R.id.delete);
username1 = (EditText)findViewById(R.id.username);
DofB = (EditText)findViewById(R.id.dateOfBirth);
password = (EditText)findViewById(R.id.password);
radioGroup = (RadioGroup)findViewById(R.id.radioBtnGroup);
update = (Button)findViewById(R.id.update);
readAll();
delete();
updateData();
}
public void updateData(){
update.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = username1.getText().toString();
String birth = DofB.getText().toString();
String pass = password.getText().toString();
int selectedID = radioGroup.getCheckedRadioButtonId();
genderRadioButton = (RadioButton)findViewById(selectedID);
String gender = genderRadioButton.getText().toString();
UserProfile.Users users = UserProfile.getProfile().getUser();
users.setUserName(username);
users.setDateOfBirth(birth);
users.setPassword(pass);
users.setGender(gender);
boolean isUpdate = myDb.updateData(users);
if(isUpdate == true){
Toast.makeText(EditProfile.this,"Data Update", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(EditProfile.this,"Data not update",Toast.LENGTH_SHORT).show();
}
}
}
);
}
public void readAll(){
viewDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = myDb.readAllInfor();
if(res.getCount() == 0){
Toast.makeText(EditProfile.this,"Error",Toast.LENGTH_SHORT).show();
}
else{
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("ID :" + res.getString(0) + "\n");
buffer.append("Username :" + res.getString(1) + "\n");
buffer.append("Date of Birth :" + res.getString(2) + "\n");
buffer.append("Password :" + res.getString(3) + "\n");
buffer.append("Gender :" + res.getString(4) + "\n");
}
showMessage("Details", buffer.toString());
}
}
});
}
public void delete(){
dele.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String uname = username1.getText().toString();
UserProfile.Users users = UserProfile.getProfile().getUser();
users.setUserName(uname);
Integer deleteRows = myDb.deleteData(users);
if(deleteRows > 0){
Toast.makeText(EditProfile.this,"Data Deleted Succesfully ", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(EditProfile.this,"Data not Deleted Succesfully",Toast.LENGTH_SHORT).show();
}
}
});
}
public void showMessage(String title,String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
这是我用来制作一个简单android程序的全部代码。我是android studio的初学者,我想知道如何更正Home.java类中的注释代码,以允许仅访问系统的注册成员。
答案 0 :(得分:0)
尝试类似这样的方法: 从列表中的db获取所有用户,或以相同的方式遍历db。
public void login(View v){
boolean exists = false;
EditText name = findViewById(R.id.username);
String nameinput = name.getText().toString();
EditText pass = findViewById(R.id.password);
String passinput = pass.getText().toString();
if (nameinput.isEmpty() || passinput.isEmpty()){
Toast.makeText(this, "Please enter Your username and password.", Toast.LENGTH_LONG);
setContentView(R.layout.activity_main);
} else {
for (User u : usersList) {
if (nameinput.equals(u.getUsername()) && passinput.equals(u.getPassword())){
exists = true;
Intent i = new Intent(this, MainMenuActivity.class);
startActivity(i);
break;
}
}
if (!exists){
Intent i = new Intent(this, RegisterActivity.class);
startActivity(i);
}
}
}