Android–資料存放區之SharedPreferences

來源:互聯網
上載者:User

android中apk中的資訊儲存主要有四種方式,SharedPreferences、File(Properties)、Network、SQLite

SharedPreferences:儲存apk中的一些配置資訊或者使用者資訊,採用索引值對方式儲存,儲存的資料類型只能是一些基礎資料型別 (Elementary Data Type)(int、string、boolean..)

今天的demo就是利用SharedPreferences儲存曆史使用者的基本資料,在登入介面內,將曆史使用者資訊綁定在AutoCompleteTextView中,實現google搜尋條的自動提示功能。

第一步:開啟登入介面,載入所有曆史使用者資訊,每一個使用者資訊用UserInfo對象儲存

 

代碼

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;
}
。。。。。
}

開始的時候想讓Properties儲存List<UserInfo>對象,但是Properties好像也只能儲存一些基礎資料型別 (Elementary Data Type),所以沒辦法退而求其次,將所有的使用者資訊儲存到String中,格式為:name1/pwd1/isRemember1,name2/pwd2/isRemember2,name3/pwd3/isRemember3。儲存方式確定下來後就可以載入資源了

 

代碼

        SharedPreferences settings=this.getPreferences(Activity.MODE_PRIVATE);
String userinfos = settings.getString(PREF_USERINFO, "");//獲得使用者字串
if(userinfos!=""){
usernames=new ArrayList<String>();//AutoCompleteTextView的資料來源
List<UserInfo> li=new ArrayList<UserInfo>();//用於儲存使用者列表資訊

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;
}

第二步:配置AutoCompleteTextView資料來源,也就是上面得到的List<String> usernames;

代碼

   ArrayAdapter<String> adapter=new ArrayAdapter<String>(this               , android.R.layout.simple_dropdown_item_1line, usernames);
actname.setAdapter(adapter);
actname.setOnItemClickListener(this);//列表選定事件,載入使用者密碼和是否記住

 

代碼

    @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){//迴圈使用者資訊列表,根據使用者名稱找到對應的詳細使用者資訊
if(user.getName().equals(name)){
String pwd=user.getPwd();
boolean bl=user.isRemember();
etpwd.setText(pwd);
ckremember.setChecked(bl);
return;
}
}
}

第三步:儲存使用者資訊

先檢查是否存在相同使用者資訊,更新使用者列表(list)資訊

 

 

 

 

代碼

    //檢查是否包含此使用者名稱
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){//已存在
list.remove(position);
}
UserInfo u=new UserInfo();
u.setName(uname);
u.setPwd(pwd);
u.setRemember(bl);
list.add(u);
}

 

 

 將終端使用者資訊一條一條儲存

代碼

    //儲存使用者資訊
private void saveUserInfo(){
checkUser();//檢查是否已存在相同使用者資訊
SharedPreferences settings=this.getPreferences(Activity.MODE_PRIVATE);
String userinfos = "";
for(UserInfo user : list){//最終儲存的使用者資訊都在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.putString(PREF_USERINFO, userinfos);
editor.commit();//編輯器提交儲存
}

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.