登录后在文本视图中显示 SQLite 数据



>我有登录活动、注册活动和个人资料活动。按下登录按钮后,我可以在个人资料活动上显示我的用户名,这是登录后的第三个活动。但是,我无法显示从数据库到个人资料活动的名字、姓氏、GRE、toefl 。vl 我如何在个人资料活动的文本视图中查看每个用户的名字、姓氏、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);
  }

}

这是我的登录名.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);

  }
 }

您需要编辑 searchPass(username, password) 方法,使其在 usernamepassword 匹配时返回完整的User对象,然后在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);
}

然后从ProfileUser活动中的intent中检索详细信息,如下所示

String username = getIntent().getStringExtra("Username");
String firstName = getIntent().getStringExtra("firstName");
String lastName = getIntent().getStringExtra("lastName");
String gre = getIntent().getStringExtra("gre");
String toefl = getIntent().getStringExtra("toefl");

最新更新