zoukankan      html  css  js  c++  java
  • Android学习之简单的数据存储

    在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>
    main.xml

    这里定义了几个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>
    base.xml

    后台代码:

     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的编码处理,在编码之前需要将其转为流的形式。

  • 相关阅读:
    struct page*
    ARM平台linux内核Notes 1
    CTDIY1字符设备驱动的使用
    深入理解linux内核读书笔记1
    在linux下设置pl2303串口
    struct per_cpu_pageset
    ARM平台linux内核Notes 2
    CTDIY2字符设备驱动的注册
    深入理解linux内核读书笔记2
    How to rollback a transaction in TSQL
  • 原文地址:https://www.cnblogs.com/ggz19/p/3880693.html
Copyright © 2011-2022 走看看