登录后在textview中显示SQLite数据

时间:2018-04-30 22:58:13

标签: java android sqlite android-activity login

我有登录活动,注册活动和个人资料活动。按下登录按钮后,我可以在登录后显示我的用户名,这是活动后的第三个活动。但是,我无法显示数据库中的名字,姓氏,GRE,toefl到资料活动。如何在配置文件活动的文本视图中查看每个用户的名字,姓氏,gre和toefl。

这是我的代码:

这是SQLiteDB ProfileDatabase.class

public class ProfileDatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION= 1;
private static final String DATABASE_NAME="profiles.db";
private static final String TABLE_NAME="profile";

private static final String COLUMN_PROFID="profId";
private static final String COLUMN_FNAME="firstName";
private static final String COLUMN_LNAME="lastName";
private static final String COLUMN_UNAME="userName";
private static final String COLUMN_PASS="pass";
private static final String COLUMN_GRE= "gre";
private static final String COLUMN_TOEFL= "toefl";
SQLiteDatabase db;

private static final String TABLE_CREATE = "create table profile (profId integer primary key  not null," +
        " firstName text not null, lastName text not null, userName text not null, pass text not null, gre integer not null, toefl integer not null);";

public ProfileDatabaseHelper(Context context) {
    super(context,DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db=db;

}

public void insertProfile(ProfileList pl){

    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "Select * from profile";
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();
    values.put(COLUMN_PROFID,count);
    values.put(COLUMN_FNAME,pl.getFName());
    values.put(COLUMN_LNAME,pl.getLName());
    values.put(COLUMN_UNAME,pl.getUName());
    values.put(COLUMN_PASS,pl.getPass());
    values.put(COLUMN_GRE,pl.getGre());
    values.put(COLUMN_TOEFL,pl.getToefl());

    db.insert(TABLE_NAME, null,values);
    db.close();

}


public String searchPass(String uname){
    db = this.getReadableDatabase();
    String query = "select username, pass from "+TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    String a, b;
    b = "Not found";
    if(cursor.moveToFirst()){
        do{
            a = cursor.getString(0);
            if(a.equals(uname)){
                b = cursor.getString(1);
                break;
            }


        }
        while(cursor.moveToNext());
    }
    return b;
}
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
    db.execSQL(query);

  }


}

这是我的login.class

public class LoginUser extends AppCompatActivity {

ProfileDatabaseHelper helper = new ProfileDatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_user);


}

public void onBtnClick(View v){
    if (v.getId() == R.id.loginButton){

        EditText loginName = (EditText)findViewById(R.id.loginUserName);
        String loginNameStr = loginName.getText().toString();

        EditText password = (EditText)findViewById(R.id.loginPass);
        String pass = password.getText().toString();

        String passwordLogin = helper.searchPass(loginNameStr);


        if(pass.equals(passwordLogin)){


            Intent i = new Intent(LoginUser.this, ProfileUser.class);
            i.putExtra("Username", loginNameStr);
            startActivity(i);
        }

        else {
            Toast.makeText(LoginUser.this,"Username and Password don't match",Toast.LENGTH_LONG).show();
        }


    }
    if (v.getId() == R.id.signUpButton){
        Intent i = new Intent(LoginUser.this, RegisterUser.class);
        startActivity(i);
    }
}
}

这是显示用户个人资料的第三个活动

public class ProfileUser extends AppCompatActivity {

ProfileDatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_user);



    String username = getIntent().getStringExtra("Username");
    TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
    userText.setText(username);

    //How canI enter each person personal first name, last name, grey and 
     toefl


    TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
    TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
    TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
    TextView userText = (TextView)findViewById(R.id.userNameDisplayText);



  }
 }

1 个答案:

答案 0 :(得分:0)

您需要修改searchPass(username, password)方法,以便在Userusername匹配时返回完整的password对象,然后在您的{之前调用此方法{1}}条件IF

现在您拥有包含所有用户详细信息的User user = helper.searchPass(loginNameStr, password);对象,您需要在检查密码后发送其他内容,如下所示。

user

然后从if(user.getPassword().equals(passwordLogin)){ Intent i = new Intent(LoginUser.this, ProfileUser.class); i.putExtra("Username", user.getUsername()); i.putExtra("firstName", user.getFirstName()); i.putExtra("lastName", user.getLastName()); i.putExtra("gre", user.getGre()); i.putExtra("toefl", user.getToefl()); startActivity(i); } 活动intent中检索详细信息,如下所示

ProfileUser