You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

登录后如何在Profile Activity的TextView中显示SQLite用户完整信息

嘿,我来帮你搞定这个问题!看起来你已经能在Profile Activity显示用户名,但还没把数据库里的其他用户信息拉出来展示对吧?咱们一步步来解决这个问题:

第一步:给你的ProfileDatabaseHelper添加正确的查询方法

首先,你的SQLite辅助类需要一个能根据登录用户名(或者用户ID,用ID更稳妥,避免用户名重复)查询完整用户信息的方法。假设你的数据库表名叫users,包含username、first_name、last_name、gre、toefl这些字段,那可以加这段代码:

// 根据用户名查询用户完整信息
public Cursor getUserByUsername(String username) {
    SQLiteDatabase db = this.getReadableDatabase();
    // 查询指定用户名的所有列数据
    return db.query(
        "users",
        null, // null表示返回所有列
        "username = ?", // 查询条件
        new String[]{username}, // 条件参数,替换?
        null, null, null
    );
}

如果你的表有唯一的user_id字段,用ID查询会更可靠,把方法改成getUserById(int userId)即可,逻辑类似。

第二步:确认登录成功后正确传递用户标识到Profile Activity

你已经能传递用户名到Profile了,这里再确认下传递逻辑没问题,比如在LoginActivity的登录按钮点击事件里:

// 登录验证通过后
Intent profileIntent = new Intent(LoginActivity.this, ProfileActivity.class);
profileIntent.putExtra("LOGGED_IN_USERNAME", inputUsername.getText().toString());
startActivity(profileIntent);
finish(); // 可选:关闭登录页,避免返回时回到登录界面
第三步:在Profile Activity中查询数据库并填充TextView

接下来在ProfileActivity的onCreate方法里,接收传递过来的用户名,查询数据库,把数据绑定到对应的TextView上:

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

    // 获取传递过来的用户名
    String loggedInUsername = getIntent().getStringExtra("LOGGED_IN_USERNAME");
    if (loggedInUsername == null) {
        // 没有拿到用户名,跳回登录页
        startActivity(new Intent(this, LoginActivity.class));
        finish();
        return;
    }

    // 初始化数据库助手
    ProfileDatabaseHelper dbHelper = new ProfileDatabaseHelper(this);
    Cursor userCursor = dbHelper.getUserByUsername(loggedInUsername);

    // 检查查询结果是否有效
    if (userCursor != null && userCursor.moveToFirst()) {
        // 从Cursor中取出对应字段的值(注意字段名要和数据库表完全一致)
        String firstName = userCursor.getString(userCursor.getColumnIndexOrThrow("first_name"));
        String lastName = userCursor.getString(userCursor.getColumnIndexOrThrow("last_name"));
        String greScore = userCursor.getString(userCursor.getColumnIndexOrThrow("gre"));
        String toeflScore = userCursor.getString(userCursor.getColumnIndexOrThrow("toefl"));

        // 找到布局里的TextView并设置文本
        TextView tvFirstName = findViewById(R.id.tv_first_name);
        TextView tvLastName = findViewById(R.id.tv_last_name);
        TextView tvGRE = findViewById(R.id.tv_gre);
        TextView tvTOEFL = findViewById(R.id.tv_toefl);

        tvFirstName.setText("First Name: " + firstName);
        tvLastName.setText("Last Name: " + lastName);
        tvGRE.setText("GRE Score: " + greScore);
        tvTOEFL.setText("TOEFL Score: " + toeflScore);

        // 记得关闭Cursor,避免内存泄漏
        userCursor.close();
    } else {
        // 没找到用户数据,提示并跳回登录页
        Toast.makeText(this, "User data not found", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }

    // 关闭数据库助手
    dbHelper.close();
}
第四步:排查几个容易踩坑的点
  • 确认数据库表结构:检查你的users表是否真的包含first_name、last_name、gre、toefl这些字段,拼写要完全一致(SQLite大小写不敏感,但统一写法更稳妥)。
  • 注册时的插入逻辑:确保Registration Activity里注册用户时,已经把这些字段正确插入到数据库了,比如插入方法要包含所有字段:
public long addUser(String username, String firstName, String lastName, String gre, String toefl, String password) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("username", username);
    values.put("first_name", firstName);
    values.put("last_name", lastName);
    values.put("gre", gre);
    values.put("toefl", toefl);
    values.put("password", password); // 注意:实际项目里密码一定要加密存储!
    return db.insert("users", null, values);
}
  • Cursor安全处理:可以用try-with-resources语法自动关闭Cursor,避免手动关闭遗漏:
try (Cursor userCursor = dbHelper.getUserByUsername(loggedInUsername)) {
    if (userCursor != null && userCursor.moveToFirst()) {
        // 处理数据逻辑
    }
}

这样操作后,Profile Activity的TextView应该就能正常展示每位用户的first name、last name、GRE和TOEFL信息啦!如果还有问题,可以用Log打印Cursor里的字段值,排查数据是否真的存在于数据库中。

内容的提问来源于stack exchange,提问作者Prathmesh Pardhiye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:54:51