Android學習之簡單的資料儲存
阿新 • • 發佈:2022-04-29
在Android中,資料儲存是開發人員不可以避免的。Android為開發者提供了很多的儲存方法,在前面的部落格中,已經講述了sqlite儲存資料。今天將介紹用SharedPreferences來儲存資料,它可以將資料儲存在應用軟體的私有儲存區,儲存區的資料只能被寫入這些資料的軟體讀取。SharedPreference通過鍵值對的方法儲存資料。
1.SharedPreference儲存簡單資料
SharedPreference可以存放簡單的String、Boolean、Int等物件。
1 <RelativeLayout xmlns:tools="http://schemas.android.com/tools" 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/textView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="姓名" /> 16 17 <EditText 18 android:id="@+id/editename" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:layout_below="@id/textView1" /> 22 23 <TextView 24 android:id="@+id/textView2" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_alignLeft="@+id/editename" 28 android:layout_below="@+id/editename" 29 android:layout_marginTop="15dp" 30 android:text="興趣愛好" /> 31 32 <EditText 33 android:id="@+id/editehoby" 34 android:layout_width="fill_parent" 35 android:layout_height="wrap_content" 36 android:layout_alignLeft="@+id/textView2" 37 android:layout_below="@+id/textView2" 38 android:layout_marginTop="21dp" 39 android:ems="10" > 40 41 <requestFocus /> 42 </EditText> 43 44 <CheckBox 45 android:id="@+id/checkBox1" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:layout_alignLeft="@+id/editehoby" 49 android:layout_below="@+id/editehoby" 50 android:layout_marginTop="18dp" 51 android:text="是否工作" /> 52 53 <TextView 54 android:id="@+id/textView3" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:layout_alignLeft="@+id/checkBox1" 58 android:layout_below="@+id/checkBox1" 59 android:layout_marginTop="22dp" 60 android:text="單位性質" /> 61 62 <RadioGroup 63 android:id="@+id/radioGroup1" 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" 66 android:layout_alignLeft="@+id/textView3" 67 android:layout_below="@+id/textView3" 68 android:layout_marginTop="24dp" > 69 70 <RadioButton 71 android:id="@+id/radio0" 72 android:layout_width="wrap_content" 73 android:layout_height="wrap_content" 74 android:checked="true" 75 android:text="國營" /> 76 77 <RadioButton 78 android:id="@+id/radio1" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:text="私營" /> 82 83 <RadioButton 84 android:id="@+id/radio2" 85 android:layout_width="wrap_content" 86 android:layout_height="wrap_content" 87 android:text="股份制" /> 88 </RadioGroup> 89 90 <Button 91 android:id="@+id/button1" 92 android:layout_width="wrap_content" 93 android:layout_height="wrap_content" 94 android:layout_alignBottom="@+id/radioGroup1" 95 android:layout_alignRight="@+id/editehoby" 96 android:layout_marginBottom="26dp" 97 android:layout_marginRight="30dp" 98 android:text="tiao" /> 99 100 </RelativeLayout>
這裡定義了幾個edittext。
1 protected void onStop() 2 { 3 //獲得SharedPreference物件 4 SharedPreferences myShared=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE); 5 //獲得editor物件 6 SharedPreferences.Editor editor=myShared.edit(); 7 //新增需要儲存的資料 8 editor.putString("name", edname.getText().toString()); 9 editor.putString("hobby", edhoby.getText().toString()); 10 editor.putBoolean("employee", cbcareer.isChecked()); 11 editor.putInt("companytype", rdCompanyType.getCheckedRadioButtonId()); 12 //提交資料 13 editor.commit(); 14 super.onStop(); 15 16 }
這對資料的儲存,並沒有放在單獨的事件中,而是放在onstop方法中。當activity停止的時候,會自動提交資料。
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_main); 4 edname=(EditText)findViewById(R.id.editename); 5 edhoby=(EditText)findViewById(R.id.editehoby); 6 cbcareer=(CheckBox)findViewById(R.id.checkBox1); 7 Button button=(Button)findViewById(R.id.button1); 8 button.setOnClickListener(new View.OnClickListener() { 9 10 @Override 11 public void onClick(View v) { 12 // TODO Auto-generated method stub 13 Intent intent=new Intent(); 14 intent.setClass(MainActivity.this, ProductSharedActivity.class); 15 startActivity(intent); 16 } 17 }); 18 rdCompanyType=(RadioGroup)findViewById(R.id.radioGroup1); 19 SharedPreferences sharedpre=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE); 20 edname.setText(sharedpre.getString("name", "")); 21 edhoby.setText(sharedpre.getString("hobby", "")); 22 cbcareer.setChecked(sharedpre.getBoolean("employee",false)); 23 rdCompanyType.check(sharedpre.getInt("companytype", -1)); 24 25 }
資料讀取與儲存的方法類似。
2.SharedPreference儲存複雜資料
SharedPreference不僅可以儲存簡單的資料,而且可以儲存複雜的資料物件,比如物件、影象等。儲存複雜的資料型別,需要對資料進行編碼。對資料的儲存方法和上面的基本一致
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" >
5
6 <TextView
7 android:id="@+id/textView1"
8 android:layout_width="wrap_content"
9 android:layout_height="wrap_content"
10 android:layout_alignParentLeft="true"
11 android:layout_alignParentTop="true"
12 android:layout_marginLeft="16dp"
13 android:layout_marginTop="15dp"
14 android:text="產品ID" />
15
16 <EditText
17 android:id="@+id/txtID"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"
20 android:layout_alignLeft="@+id/textView1"
21 android:layout_below="@+id/textView1"
22 android:layout_marginTop="25dp"
23 android:ems="10" >
24
25 <requestFocus />
26 </EditText>
27
28 <TextView
29 android:id="@+id/textView2"
30 android:layout_width="wrap_content"
31 android:layout_height="wrap_content"
32 android:layout_alignLeft="@+id/txtID"
33 android:layout_below="@+id/txtID"
34 android:layout_marginTop="18dp"
35 android:text="產品名稱" />
36
37 <EditText
38 android:id="@+id/txtName"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content"
41 android:layout_alignLeft="@+id/textView2"
42 android:layout_below="@+id/textView2"
43 android:layout_marginTop="32dp"
44 android:ems="10" />
45
46 <TextView
47 android:id="@+id/textView3"
48 android:layout_width="wrap_content"
49 android:layout_height="wrap_content"
50 android:layout_alignLeft="@+id/txtName"
51 android:layout_centerVertical="true"
52 android:text="產品價格" />
53
54 <EditText
55 android:id="@+id/txtprice"
56 android:layout_width="wrap_content"
57 android:layout_height="wrap_content"
58 android:layout_alignLeft="@+id/textView3"
59 android:layout_below="@+id/textView3"
60 android:layout_marginTop="28dp"
61 android:ems="10" />
62
63 <ImageView
64 android:id="@+id/imageView1"
65 android:layout_width="wrap_content"
66 android:layout_height="wrap_content"
67 android:layout_below="@+id/txtprice"
68 android:layout_toRightOf="@+id/textView3" />
69
70 <Button
71 android:id="@+id/button1"
72 android:layout_width="wrap_content"
73 android:layout_height="wrap_content"
74 android:layout_alignRight="@+id/textView3"
75 android:layout_below="@+id/imageView1"
76 android:layout_marginTop="34dp"
77 android:text="選擇影象" />
78
79 </RelativeLayout>
後臺程式碼:
1 protected void onStop()
2 {
3 Product product=new Product();
4 /*product.setID(edid.getText().toString());
5 product.setName(edname.getText().toString());
6 product.setPrice(edprice.getText().toString());*/
7 product.productname=edname.getText().toString();
8 product.productid=edid.getText().toString();
9 product.productprice=edprice.getText().toString();
10
11 ByteArrayOutputStream baos=new ByteArrayOutputStream();
12 ObjectOutputStream oos;
13 ((BitmapDrawable)imageview.getDrawable()).getBitmap().compress(CompressFormat.JPEG, 50, baos);
14 String imagebase=new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
15 try {
16 oos = new ObjectOutputStream(baos);
17 oos.writeObject(product);
18 } catch (IOException e) {
19 // TODO Auto-generated catch block
20 e.printStackTrace();
21 };
22 //獲得SharedPreference物件
23 SharedPreferences myshared=getSharedPreferences("base64", Activity.MODE_PRIVATE);
24 String productbase=new String(Base64.encode(baos.toByteArray(),Base64.DEFAULT));
25 //獲得editor物件
26 SharedPreferences.Editor editor=myshared.edit();
27 //新增需要儲存的資料
28 editor.putString("product", productbase);
29 editor.putString("productimage", imagebase);
30 //提交儲存資料
31 editor.commit();
32
33 super.onStop();
34 }
這裡需要儲存的資料都經過了base64的編碼處理,在編碼之前需要將其轉為流的形式。