如果密码与用户名(由用户输入)匹配,如何在textview上显示密码?

时间:2014-01-15 10:55:32

标签: android

这是我的数据库代码

public class DBHelper extends SQLiteOpenHelper {

    private SQLiteDatabase db;

    //Column for registration table
    public static final String KEY_ROWID = "_id";
    public static final String KEY_FNAME = "firstname";
    public static final String KEY_LNAME = "lastname";
    public static final String KEY_GENDER = "gender";
    public static final String KEY_USER = "username";
    public static final String KEY_EMAIL = "email";

    DBHelper DB = null;
    public static final String DATABASE_NAME = "srikanth1.db";
    public static final int DATABASE_VERSION = 2;
    public static final String DATABASE_TABLE_NAME = "sri1";

    private static final String DATABASE_TABLE_CREATE = "CREATE TABLE " +    DATABASE_TABLE_NAME + "(" +
            "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL," +
                    " gender TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, " +
                    "email TEXT NOT NULL);";

    public DBHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        System.out.println("In constructor");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {  
            db.execSQL(DATABASE_TABLE_CREATE);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }

    public Cursor rawQuery(String string, String[] strings) {
    // TODO Auto-generated method stub
        return null;
    }

    public void open() {

        getWritableDatabase(); 
    }

    public Cursor getDetails(String text) throws SQLException {

        Cursor mCursor = db.query(true, DATABASE_TABLE_NAME, 
              new String[]{KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER, KEY_USER, KEY_EMAIL}, 
              KEY_USER + "=" + text, null, null, null, null, null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

}

和我的loginActivity代码

public class LoginActivity extends Activity implements OnClickListener {

    Button mLogin;
    Button mRegister; 
    EditText muname;
    EditText mpassword;

    DBHelper DB = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mRegister = (Button)findViewById(R.id.register);
        mRegister.setOnClickListener(this);

        mLogin = (Button)findViewById(R.id.login);
        mLogin.setOnClickListener(this);         
    }

    public void onClick(View v) {

        switch(v.getId()) {

        case R.id.register:

            Intent i = new Intent(getBaseContext(), Registration.class);
            startActivity(i);
            break;

        case R.id.login:

            muname = (EditText)findViewById(R.id.Ledituname);
            mpassword = (EditText)findViewById(R.id.Leditpw);

            String username = muname.getText().toString();
            String password = mpassword.getText().toString();

            if(username.equals("") || username == null) {

                Toast.makeText(getApplicationContext(), "Please enter User Name", Toast.LENGTH_SHORT).show();
            }

            else if(password.equals("") || password == null) {

                Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
            }

            else {

                boolean validLogin = validateLogin(username, password, getBaseContext());

                if(validLogin) {

                    Intent in = new Intent(getBaseContext(), Welcome1.class);
                    in.putExtra("UserName", muname.getText().toString());
                    startActivity(in);   
                }
            }
            break;          
        }  
    }


    @SuppressWarnings("deprecation")
    private boolean validateLogin(String username, String password, Context baseContext) {

        DB = new DBHelper(getBaseContext());
        SQLiteDatabase db = DB.getReadableDatabase();

        String[] columns = {"_id"};

        String selection = "username=? AND password=?";
        String[] selectionArgs = {username,password};

        Cursor cursor = null;

        try {  
            cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection, selectionArgs, null, null, null);
            startManagingCursor(cursor);
        } catch(Exception e) {
                e.printStackTrace();
          }

        int numberOfRows = cursor.getCount();

        if(numberOfRows <= 0) {

            Toast.makeText(getApplicationContext(), "User Name and Password miss match..\nPlease Try Again", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(getBaseContext(), LoginActivity.class);
            startActivity(intent);

          return false;
        }

        return true;
   }

    public void onDestroy() {

        super.onDestroy();
        DB.close();
    }
}

我的故事是用户点击“忘记密码”,然后会显示一个对话框,用户将在其中输入他/她的用户名并单击“查找”按钮,然后它应该在textview上显示数据库中的相应密码。请任何身体帮助......谢谢

3 个答案:

答案 0 :(得分:0)

您可以使用以下方法更改TextView的输入类型:

yourtextView.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);

答案 1 :(得分:0)

使用原始查询

String pass = "select password from "+DATABASE_TABLE_NAME +" where firstname = '"+NameSelectedByUser+"'";

答案 2 :(得分:0)

使用此查询选择密码

String password =“SELECT DASSABORD FROM DATABASE WHERE USERNAME ='”+ username +“'”;

并在textview上设置此密码。

希望这有助于你。