Android-sharedpreferences for data storage

Source: Internet
Author: User

In Android, there are four main ways to save APK information: sharedpreferences, file (properties), network, SQLite

Sharedpreferences: stores configuration information or user information in the APK and uses key-value pairs. the stored data type can only be basic data types (INT, String, Boolean ..)

Today's demo uses sharedpreferences to save the basic information of historical users. In the logon interface, historical user information is bound to autocompletetextview to enable the automatic prompt function of Google search entries.

Step 1: Open the logon interface and load all historical user information. Each user information is saved as a userinfo object.

 

Code

 public class UserInfo{
private String name="";
private String pwd="";
private boolean isRemember=false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
。。。。。
}

At the beginning, you want properties to save the list <userinfo> object. However, properties seems to only save some basic data types, so you cannot leave the second step, save all user information to string in the format of name1/pwd1/isremember1, name2/pwd2/isremember2, name3/pwd3/isremember3. After the storage method is determined, the resource can be loaded.

 

Code

 Sharedpreferences settings = This. getpreferences (activity. mode_private );
String userinfos = settings. getstring (pref_userinfo, ""); // obtain the user string
If (userinfos! = ""){
Usernames = new arraylist <string> (); // data source of autocompletetextview
List <userinfo> li = new arraylist <userinfo> (); // used to save user list information

If (userinfos. Contains (",")){
String [] users = userinfos. Split (",");
For (string STR: Users ){
Userinfo u = new userinfo ();
String [] user = Str. Split ("/");
U. Name = user [0];
U. Pwd = user [1];
U. isremember = Boolean. parseboolean (User [2]);
Usernames. Add (User [0]);
Li. Add (U );
}
} Else {
Userinfo u = new userinfo ();
String [] user = userinfos. Split ("/");
U. Name = user [0];
U. Pwd = user [1];
U. isremember = Boolean. parseboolean (User [2]);
Usernames. Add (User [0]);
Li. Add (U );
}
Return Li;
} Else {
Return NULL;
}

Step 2: configure the autocompletetextview data source, that is, the list <string> usernames obtained above;

Code

 Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_dropdown_item_1line, usernames );
Actname. setadapter (adapter );
Actname. setonitemclicklistener (this); // list selected events, load user passwords, and remember

 

Code

 @ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3 ){
// Todo auto-generated method stub
Log. V (TAG, "----------- item selected ");
String name = This. actname. gettext (). tostring (). Trim ();
For (userinfo User: List) {// list of cyclic user information, find the corresponding detailed user information based on the user name
If (user. getname (). Equals (name )){
String Pwd = user. getpwd ();
Boolean BL = user. isremember ();
Etpwd. settext (PWD );
Ckremember. setchecked (BL );
Return;
}
}
}

Step 3: Save User Information

First, check whether the same user information exists and update the user list information.

 

 

 

 

Code

 // Check whether the user name is included
Public void checkuser (){
String uname = actname. gettext (). tostring (). Trim ();
String Pwd = etpwd. gettext (). tostring (). Trim ();
Boolean BL = ckremember. ischecked ();
Int position = 0;
For (INT I = 0; I <list. Size (); I ++ ){
If (list. Get (I). getname (). Equals (uname )){
Position = I;
Break;
}
}
If (position> = 0) {// already exists
List. Remove (position );
}
Userinfo u = new userinfo ();
U. setname (uname );
U. setpwd (PWD );
U. setremember (BL );
List. Add (U );
}

 

 

Save end user information one by one

Code

 // Save user information
Private void saveuserinfo (){
Checkuser (); // check whether the same user information already exists
Sharedpreferences settings = This. getpreferences (activity. mode_private );
String userinfos = "";
For (userinfo User: List) {// The final Stored User information is in list
String uname = user. getname ();
String Pwd = user. getpwd ();
String userinfo = uname + "/" + PWD;
If (user. isremember ()){
Userinfo + = "/true ";
} Else {
Userinfo + = "/false ";
}
If (userinfos = ""){
Userinfos = userinfo;
} Else {
Userinfos + = "," + userinfo;
}
}
Editor editor = settings. Edit (); // editor record
Editor. putstring (pref_userinfo, userinfos );
Editor. Commit (); // submit and save the editor
}

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.