- Forum posts: 2
Apr 4, 2016, 9:35:30 PM via Website
Apr 4, 2016 9:35:30 PM via Website
Hi, I am a beginner at programming and android and everything really but my course likes to throw me in the deep end. Anyway have an android app that a user can create a user account and will save the values in an SQLite datebase, those values are username and password. My question is I need to add a page to my android app that will allow the users to change the password meaning they need to change the password in the database and this is where I get completely lost and could use some guidance.
Here is my ChangePassword Activity that currently has nothing of great use as I am so lost.
public void onClickUpdate(View v) {
if (v.getId() == R.id.Bupdate) {
//Take all text field values
EditText findUname = (EditText) findViewById(R.id.TFfindusername);
EditText newPass1 = (EditText) findViewById(R.id.TFnewpassword);
EditText newPass2 = (EditText) findViewById(R.id.TFverifynewpassword);
// Converts them to strings
String newunamestr = findUname.getText().toString();
String newpass1str = newPass1.getText().toString();
String newpass2str = newPass2.getText().toString();
//Check if passwords match
if (!newpass1str.equals(newpass2str)) {
// Show Toast Message
Toast pass = Toast.makeText(ChangePassword.this, "Passwords Do Not Match!", Toast.LENGTH_SHORT);
pass.show();
// Clears Text fields if passwords do not match
newPass1.setText("");
newPass2.setText("");
} else {
// Code to update password goes here
Toast created = Toast.makeText(ChangePassword.this, "Password Successfuly Changed", Toast.LENGTH_SHORT);
created.show();
}
}
}
I need to add stuff to the else statement that will allow me to update the password in the database with the new password which is newpass1str. Ignore the password 2 part that is just for button click validation.
That activity communicates to the database using the following line at the top of the activity.
DatabaseHelper helper = new DatabaseHelper(this);
Below is my database page that creates the database etc.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
"uname text not null , pass text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
} //Ends DatabaseHelper Constructor
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
} // Ends onCreate
public void insertContact(Contact c) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from contacts";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID, count);
values.put(COLUMN_UNAME, c.getUname());
values.put(COLUMN_PASS, c.getPass());
// Inserts values
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String uname) {
db = this.getReadableDatabase();
String query = "select uname, pass from " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
if (cursor.moveToFirst()) {
do {
a = cursor.getString(0); // Username
if (a.equals(uname)) {
b = cursor.getString(1); // Password
break;
}
}
while (cursor.moveToNext());
}
return b;
} // Ends searchPass
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
} // Ends onUpgrade
} // Ends DatabaseHelper
I will say I followed a tutorial for the that code above and I managed some slight changes but I don't understand any of it enough to work it out and the guides online completely go over my head. What would I need to add to both of these activities for me to be able to change the password for the user mentioned in the change password form. If you need any more information or I am in the wrong place and stupid please don't hesitate to tell me.
Oh I don't think this page is useful here but I also have an activity called contact which is used for the database would I need to add anything to that.
public class Contact {
int id;
String uname, pass;
// Set the ID
public void setId(int id) {
this.id = id;
}
// Get the ID
public int getId() {
return this.id;
}
// Set the Username
public void setUname(String uname) {
this.uname = uname;
}
// Get the Username
public String getUname() {
return this.uname;
}
// Set the Password
public void setPass(String pass) {
this.pass = pass;
}
// Get the Password
public String getPass() {
return this.pass;
}
} // Ends Contact
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.