如何从sqlite数据库中检索数据并在“编辑文本框”中显示

时间:2014-03-19 18:32:33

标签: android android-layout android-intent android-service android-sqlite

我有一个应用程序

1:用户注册她的ID和密码,

2:然后在第二个屏幕上他签到。

3成功登录后,用户导航到可以更改密码的屏幕

问题陈述:我希望当用户点击更改密码按钮然后他导航到注册屏幕,在这里用户名编辑文本他可以看到他的名字,在那里他可以更改他的密码任何解决方案将不胜感激。

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});
        }
} 

在我的主要活动上,我想要检索用户名

1 个答案:

答案 0 :(得分:0)

在你的活动电话中

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

现在在您的DAtabase类中添加此方法

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(); 
} 

}

希望这适合你:)