0

I have an application in which

1:user sign Up her ID and password,

2: then on 2nd screen he sign In.

3 After successful log in user navigate to the screen from where he can change his password

Problem statement: I want that when user clicks on Change password button then he navigates to signup screen and here in User Name edit text he could see his name and there he may change his Password Any solution will be appreciated.

DB:

    public class LoginDataBaseAdapter {
    static final String DATABASE_NAME="login.db";
    static      final int DATABASE_VERSION=1; 
    public static final int NAME_COLUMN=1;

    static final String  DATABASE_CREATE= "CREATE TABLE " + " LOGIN " + "(" + " ID " + " INTEGER PRIMARY KEY AUTOINCREMENT," + " USERNAME text,PASSWORD text);";

    public SQLiteDatabase db; 

    private final Context context;
    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)
        { 
            ContentValues newValues=new ContentValues(); 
            newValues.put("USERNAME", userName);
            newValues.put("PASSWORD", password); 
            db.insert("LOGIN", null, newValues); 

            Toast.makeText(context,"Successfully created", Toast.LENGTH_LONG).show();
        } 

    public int deleteEntry(String UserName) 
        { 
            String id=String.valueOf(id); 
            String where="USERNAME ?"; 

            int numberOFEnteriesDeleted=db.delete("LOGIN",where,new String[]{UserName});

            Toast.makeText(context," Deleted Successfully:"+numberOFEnteriesDeleted, Toast.LENGTH_LONG).show();

            return numberOFEnteriesDeleted;
        } 

    public String getSingleEntry(String userName)
        { 
            Cursor cursor=db.query("LOGIN",null, "USERNAME=?",new String[]{userName}, null, null,null);

            if (cursor.getCount()<1) 
                { 
                    cursor.close(); 
                    return "NOTEXIST";
                } 

            cursor.moveToFirst();
            String password =cursor.getString(cursor.getColumnIndex("PASSWORD")); 
            cursor.close(); 
            return password;
        } 

    public void updateEntry(String userName,String password)
        { 
            ContentValues updateValues=new ContentValues(); 
            updateValues.put("USERNAME", userName); 
            updateValues.put("PASSWORD", password); 
            String where="USERNAME=?"; 
            db.update("LOGIN", updateValues, where, new String[]{userName});
        }
} 

On my Main Activity i want to retrive the USER Name

Carlos
  • 388
  • 2
  • 13
  • 33
Bushra
  • 21
  • 7
  • 1
    Why do you need SQLite to do this. You could store user name in sharedpreferences much more easily. – Rarw Mar 19 '14 at 18:44
  • i have done all the work of sqlit databases but now i am unable to set my logics that how to get the vale in edit textbox – Bushra Mar 19 '14 at 18:45
  • post the code for your SQLite database class and where you're trying to set it to edit text – Rarw Mar 19 '14 at 18:49
  • I made some change to the format of the post, please add some line breaks to improve your code readability – Axxiss Mar 19 '14 at 19:09
  • here in stackover flow it gets like that ...as m new here – Bushra Mar 19 '14 at 19:14
  • @Bushra : its easy ! at the time of registering the user use shared preferences instead of a database. Store username password in the shared prefs . At the time when you want change the password , you can simply edit the shared prefrences. It is less complicated then handling a database and takes less space. – Udit Kapahi Mar 19 '14 at 19:25
  • @ Bushra : to get the username at time of changing password , you can check your shared preferences whether some name exists or not , if name exists i.e. name not null in preferences , fetch the value from preferences and display it in the EDITTEXT. .... Hope this may help you out. – Udit Kapahi Mar 19 '14 at 19:28
  • @UditKapahi I WANT TO DO IN THAT CASE – Bushra Mar 19 '14 at 19:29
  • In the case ,SQLITE, you need to just add one more function that only returns the name from your database ,in your LOginDatabaseAdapter class. – Udit Kapahi Mar 19 '14 at 19:31
  • HOW TO MAINTAIN THAT? THANKU – Bushra Mar 19 '14 at 19:34
  • Read this http://developer.android.com/reference/android/content/SharedPreferences.html and this http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Carlos Mar 19 '14 at 19:50

1 Answers1

0

in your activity call

LoginDataBaseAdapter obj=new LoginDataBaseAdapter(); 
obj.getReadableDatabase(); 
String user=obj.getUSername(); 

Now in your DAtabase class add this method

class LoginDatabaseAdapter(){ 
Cursor c; 
String userame; 
SQLiteDatabase db; 
public String getUserName(){ 
db=this.getReadableDatabase(); 
c=db.query(TABLE_NAME, new String[]{KEY_USERNAME}, null, null, null, null, null); 
if(c!=null){ 
c.moveToFirst(); 
username=c.getString(0); 
}
c.close(); 
db.close(); 
return username(); 
} 

}

Hope this works for you :)

Udit Kapahi
  • 2,277
  • 1
  • 27
  • 25