android - 通过 Shared Preference 从 Sugar ORM 中检索数据

标签 android database sqlite sugarorm

我正在尝试在我的 Android 应用程序上创建更新用户信息 fragment 。在我的登录页面上,我要求用户填写他/她的电子邮件和密码以进入系统。然后,一旦我到达用户信息 fragment ,我想显示有关用户的所有信息。因此,我在我的登录页面上设置了对我的电子邮件的共享偏好,并且我在我的用户信息 fragment 中得到了它。现在我需要弄清楚如何使用这封电子邮件从我的数据库中检索数据。我正在使用 Sugar ORM。有什么方法可以让我获取与电子邮件地址对应的数据。例如:名字、姓氏等。不幸的是,我下面的查询不起作用。

SharedPreferences sharedpreferences;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
   View rootView= inflater.inflate(R.layout.profile, container, false);
   TextView emailLabel=(TextView) rootView.findViewById(R.id.textView3);
    TextView nameLabel=(TextView) rootView.findViewById(R.id.textView4);
    sharedpreferences = getContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    String email=sharedpreferences.getString(Email,"");
    List<User>user_email=User.findWithQuery(User.class,"SELECT * from USER WHERE email = ?",email);
    emailLabel.setText(email);
    nameLabel.setText(user_email);

    return rootView;
}

这是我的 sugar ORM 模型类

public class User extends SugarRecord<User> {
String fistname;
String lastname;
String email;
String password;
String confirmpassword;

public User() {
}

public User(String fistname, String lastname, String email, String password, String confirmpassword) {
    this.fistname = fistname;
    this.lastname = lastname;
    this.email = email;
    this.password = password;
    this.confirmpassword = confirmpassword;
}
}

最佳答案

想通了伙计们!

    List<User> user = User.findWithQuery(User.class,"SELECT * FROM USER WHERE email = ?",email);
    for (User users:user){
        nameLabel.setText(users.getFistname().toString());
    }

关于android - 通过 Shared Preference 从 Sugar ORM 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50431884/

相关文章:

android - 在后台长按硬件媒体按钮

mysql - 添加多个项目的数据库设计

mysql - 为什么 MySQL 查询在开发环境和实时环境之间运行不同?

.net - Entity Framework "Cannot insert explicit value for for identity column"

android - 我可以在 facebook 或其他支持 oauth2 标准的服务中使用 google-api-java-client 库进行 oauth2 授权吗?

java - Frida hook 重写匿名类的方法?

android - 在 Android 上显示标记弹出窗口, native 方式

sqlite - Xamarin Forms、Sqlite、EF Core 3、迁移和很多困惑

iphone - sqlite 只返回一条记录

c# - 使用 SQLite-Net Extensions 和 OneToMany 关系