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

2019-09-15 09:45发布

I'm coding an Android application. 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. What is the best way to do this?

3条回答
可以哭但决不认输i
2楼-- · 2019-09-15 10:04

You can Use Shared Preference;

Check this tutorial.

查看更多
冷血范
3楼-- · 2019-09-15 10:11

You can use Shared Preferences to store 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

查看更多
啃猪蹄的小仙女
4楼-- · 2019-09-15 10:20

There are 4 ways Some of the below methods are useful if you want to save more complex data.

1.Shared Preferences-best for your use case

Find the tutorial here

2.SQLite-a database used for android

Check this tutorial to get through SQLite

3.Content providers, if you want to share the same local data to other applications of your choice

Find the tutorial here

4.ORM mapping libraries

a.GreenDao

my favorite-Documentation and HowToDo

b.DBFlow

little complex but fast-Documentation and HowToDo

c.ORMLite

Very good documentation and support over the internet-Documentation and How to do

d.Realm

New but heavily used by tech giants-Documentation and how to do

查看更多
登录 后发表回答