简体   繁体   English

在本地存储ID的最佳方法是什么?

[英]What's the best way to store an id in local?

I'm coding an Android application. 我正在编写一个Android应用程序。 I need to store the id of a user in local in order that he doesn't have to rewrite its name, surname and password every time he uses the application. 我需要将用户的ID存储在本地,以便他不必在每次使用该应用程序时都重写其名称,姓氏和密码。 What is the best way to do this? 做这个的最好方式是什么?

You can Use Shared Preference; 您可以使用共享首选项;

Check this tutorial. 查看教程。

You can use Shared Preferences to store id 您可以使用共享首选项存储ID

Code to save value 节省价值的代码

 public static final String MyPREFERENCES = "MyPrefs" ;

 SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

 SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putString("id", 1);
      /*editor.putString("Key", value); */
        editor.commit();

Code to get value 获得价值的代码

 String id = sharedpreferences.getString("id", "");
/*sharedpreferences.getString("key", "defaultValue"); */

Hope this will help you 希望这个能对您有所帮助

There are 4 ways Some of the below methods are useful if you want to save more complex data. 有4种 方法如果要保存更复杂的数据,以下某些方法很有用。

1.Shared Preferences-best for your use case 1.共享首选项-最适合您的用例

Find the tutorial here 在这里找到教程

2.SQLite-a database used for android 2.SQLite-用于Android的数据库

Check this tutorial to get through SQLite 查看本教程以了解SQLite

3.Content providers, if you want to share the same local data to other applications of your choice 3.Content提供商,如果您想与您选择的其他应用程序共享相同的本地数据

Find the tutorial here 在这里找到教程

4.ORM mapping libraries 4.ORM映射库

a.GreenDao 绿岛

my favorite- Documentation and HowToDo 我最喜欢的- 文档和HowToDo

b.DBFlow b。数据库流

little complex but fast- Documentation and HowToDo 有点复杂但是很快-文档和操作方法

c.ORMLite c.ORMLite

Very good documentation and support over the internet- Documentation and How to do 互联网上非常好的文档和支持- 文档和操作方法

d.Realm d。境界

New but heavily used by tech giants- Documentation and how to do 新技术,但被技术巨头广泛使用- 文档以及操作方法

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

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