简体   繁体   English

从数据库中检索数据以使用房间显示用户配置文件信息

[英]Retrieving data from database to display user profile information using room

I have set up a login and signup system but I need a profile page to print the information of that user and I am struggling with it.我已经设置了一个登录和注册系统,但我需要一个个人资料页面来打印该用户的信息,我正在努力解决这个问题。 So far I have created the page and where I want it all to be printed.到目前为止,我已经创建了页面以及我希望将其全部打印的位置。 The code I have so far is:我到目前为止的代码是:

package com.example.moneymanagment;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.moneymanagment.models.Users;

public class ProfileActivity extends AppCompatActivity {

    private TextView name, surname, email, expenditure, leftover;
    private Users users;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_activity);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        users = (Users) getIntent().getSerializableExtra("Users");

        name = findViewById(R.id.tvFname);
        surname = findViewById(R.id.tvSname);
        email = findViewById(R.id.tvEmail);
        expenditure = findViewById(R.id.tvExpenditure);
        leftover = findViewById(R.id.leftover);

        if (users != null) {
            name.setText(users.getName());
            surname.setText(users.getLastName());
            email.setText(users.getEmail());
        }else {
            name.setText(users.toString());
        }

    }
}

After testing it, I have found that it is not meeting the if condition;测试后发现不满足if条件; the user is currently at null despite me logging in. I have another activity in my project that displays the user's name after the login and I used that code as a reference but it does not seem to work.尽管我登录了,但该用户目前在 null。我的项目中有另一个活动在登录后显示用户名,我使用该代码作为参考,但它似乎不起作用。

Please let me know if you need more information.如果您需要更多信息,请告诉我。 Thanks, guys谢谢你们

Is your Users class implementing Serializable?您的用户 class 是否实现了可序列化? It must do so in order to deserialize the "Users" extra from the intent它必须这样做才能从意图中反序列化额外的“用户”

If I understand correctly you want to know how to use Room to store the Users data.如果我理解正确,您想知道如何使用 Room 来存储用户数据。 This is all on the android developers website.这一切都在 android 开发者网站上。 Here you can see how to use Room in your project.在这里,您可以了解如何在您的项目中使用 Room。 Andhere you can see some resources that will help you to better understand how to use Room database. 在这里您可以看到一些资源,可以帮助您更好地了解如何使用 Room 数据库。 They explained it really well, but if you have any doubts you can ask me and I will try to help you.他们解释得很好,但如果您有任何疑问,可以问我,我会尽力帮助您。 I used Room to make a database with more than 10 tables so I have research the topic a lot in the last few months我使用 Room 创建了一个包含 10 多个表的数据库,所以在过去几个月里我对这个主题进行了很多研究

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM