mardi 17 février 2015

How to compare database and edit text values in android

I am creating android application that consists of registration form using SQLite database.For Signup, I created one popup registration form successfully stored the values in database.In this I placed a Text view component called "FORGOT PASSWORD".By clicking this a Dialog will pop up for resetting of passwords will be done by entering new passwords.In this resetting passwords dialog I am having a Edit text field called "username".**My requirement is After entering username and new passwords clicking on "reset" button user name filed will compare the names that was registered on database names and update passwords that was matched with the database name in the passwords filed.**I tried it but it was not working please help me how to do it.This is my activity code after clicking forgot password:



forgot_pwd = (TextView)findViewById(R.id.txt_forgot_pwd);
forgot_pwd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{

android.app.Dialog forgot_dialog = new android.app.Dialog(Login_Page.this);
forgot_dialog.setContentView(R.layout.forgot_pwd_layout);
forgot_dialog.setTitle("Forgot password");

forgot_new_pwd = (EditText)forgot_dialog.findViewById(R.id.editText_new_password);
confirm_pwd_forgot = (EditText)forgot_dialog.findViewById(R.id.editText_confirm_new_password);
username_forgot = (EditText)forgot_dialog.findViewById(R.id.editText_forgot_username);
forgot_username_string = username_forgot.getText().toString();
forgot_new_password_string = forgot_new_pwd.getText().toString();
forgot_confirm_pasword_string = confirm_pwd_forgot.getText().toString();
forgot_clear = (ButtonFlat)forgot_dialog.findViewById(R.id.dialog_button_clear_forgot);
forgot_reset = (ButtonFlat)forgot_dialog.findViewById(R.id.dialog_button_Reset_forgot);
forgot_reset.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
if(username_forgot.equals(dialog_user_name_string))
{
loginDataBaseAdapter.deleteEntry(dialog_pasword_string);
loginDataBaseAdapter.updateEntry( dialog_pasword_string);
}
else
{
Toast.makeText(Login_Page.this, "Please enter correct username", Toast.LENGTH_LONG).show();
}

}
});


This is my database adapter class:



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 UNIQUE,PASSWORD 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 dialog_user_name_string,String dialog_pasword_string,String Spinnerselection,String dialog_seq_answer)
{
ContentValues newValues = new ContentValues();

newValues.put("USERNAME", dialog_user_name_string);
newValues.put("PASSWORD",dialog_pasword_string);
newValues.put("SpinnerValue", Spinnerselection);
newValues.put("SpinnerValue", dialog_seq_answer);
// 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 dialog_pasword_string)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{dialog_pasword_string}) ;
// 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 forgot_new_password_string)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
//updatedValues.put("USERNAME", dialog_user_name_string);
updatedValues.put("PASSWORD",forgot_new_password_string);


String where="PASSWORD = ?";
db.update("LOGIN",updatedValues, where, new String[]{forgot_new_password_string});
}
}

Aucun commentaire:

Enregistrer un commentaire