安卓小白开发之学习安卓的点点滴滴—数据库(1)

9 篇文章 0 订阅
4 篇文章 0 订阅

安卓学习笔记之数据的简单使用与案例

小白的安卓开发(不喜勿喷,有错可以指出,第一篇博文)

 

安卓数据库和数据的储存与数据的应用,总的来说通过3种方式:

文件储存、SharedPreference储存、数据库储存,下面一一介绍。

1, 文件储存

概述:与Java的io流相类似,安卓的文件储存也是通过各种Stream,reader,writer来完成文件储存的一系列操作,只不过其中多了两种设置管道的方法——openFileOutPut(),和openFileInput()。

 

方法&属性介绍:

openFileOutput()方法:

定义类:Context

特性:静态方法

参数:文件名(字符串),操作模式(Context下的常量)

返回值:FileOutputStream

 

操作模式:

MODE_PRIVATE

定义类:Context

特性:静态常量

意义:默认操作模式,当当前指定的文件名在系统中已经存在时,该文件会被覆盖。

 

MODE_APPEND

定义类:Context

特性:静态常量

意义:当文件存在时在文件后面追加数据,在文件不存在时该文件会被创建。

 

openFileInput()方法:

特性:静态方法

参数:文件名(字符串)

返回值:FileInputStream

 

注意:上述两个方法,都是与Java中的io流类似的,所以必须包裹try,catch。

推荐使用:BufferedReader/Writer套OutputStreamReader/Writer套openFileInput/Output()

 

查看data存储的方式:

DDMS视图->File Explorer视图->data->data->project名->所见文件名

如图,上图data就是所生成的文件

案例:

实现文本框中写数据,当点击按钮时,数据写入,当转换到相应页面时显示所输入的数据。

 

实现:new 一个project ReadAndWrite

首先在ui界面中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">

 

<EditText

    android:id="@+id/content_in"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:ems="10"

    />

<Button

    android:id="@+id/does"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="输入"

    android:layout_marginTop="80dp"

    android:layout_marginLeft="100dp"

    />

 

 

</RelativeLayout>

 

 

MainActivity中:

package com.example.writeandread;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

 

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.Window;

import android.widget.TextView;

 

public class SecondActivity extends Activity{

 

       int count=MainActivity.count;

       TextView tv;

       @Override

       protected voidonCreate(Bundle savedInstanceState) {

              // TODOAuto-generated method stub

              requestWindowFeature(Window.FEATURE_NO_TITLE);

              setContentView(R.layout.content_view);

              tv=(TextView)findViewById(R.id.content_out);

              try{

                     BufferedReaderin=new BufferedReader(new InputStreamReader(openFileInput("data1")));

                     Stringstr="";

              while(--count>=0){

                     str=in.readLine();

              }

              tv.setText(str);

             

              } catch(FileNotFoundException e) {

                     //TODO Auto-generated catch block

                     e.printStackTrace();

              } catch(IOException e) {

                     //TODO Auto-generated catch block

                     e.printStackTrace();

              }

              super.onCreate(savedInstanceState);

       }

       public staticvoid actionStart(Activity a){

              Intenti=new Intent(a,SecondActivity.class);

              a.startActivity(i);

       }

 

}

简要说明:

简单的在界面中放置了一个文本输入框和一个按钮用来提示:

1, 可以讲输入写入文件了。

2, 可以转换页面了,让我们看看文件中都写入了怎样的数据。

 

然后MainActivity中设置了上面所讲的writer

调用方法写入文件。

 

接下来创建第二个activity用来显示玩呢建中的内容,同时也呈现reader的效果

 

首先也是创建UI界面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

   

<TextView

    android:id="@+id/content_out"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:textSize="20sp"

    android:gravity="center"

    android:text="20"

    />

</LinearLayout>

 

SecondActivity:

package com.example.writeandread;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

 

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.Window;

import android.widget.TextView;

 

public class SecondActivity extends Activity{

 

       int count=MainActivity.count;

       TextView tv;

       @Override

       protected voidonCreate(Bundle savedInstanceState) {

              // TODOAuto-generated method stub

              requestWindowFeature(Window.FEATURE_NO_TITLE);

              setContentView(R.layout.content_view);

              tv=(TextView)findViewById(R.id.content_out);

              try{

                     BufferedReaderin=new BufferedReader(new InputStreamReader(openFileInput("data1")));

                     Stringstr="";

              while(--count>=0){

                     str=in.readLine();

              }

              tv.setText(str);

             

              } catch(FileNotFoundException e) {

                     //TODO Auto-generated catch block

                     e.printStackTrace();

              } catch(IOException e) {

                     //TODO Auto-generated catch block

                     e.printStackTrace();

              }

              super.onCreate(savedInstanceState);

       }

       public staticvoid actionStart(Activity a){

              Intenti=new Intent(a,SecondActivity.class);

              a.startActivity(i);

       }

 

}

 简要说明:

在UI界面中简单的放置了一个TextView用来呈现要打印的文本

同时在SecondActivity防止了相应的流出管道in

从而让数据得以呈现

 

 

如此输入read and write


输出结果:

 

如此实现了:read and write更完成了对安卓文件类型数据的认识。

 

 

优势:较简单,快捷,贴近Java,多个程序或者活动访问方便,路径较为绝对。

 

劣势 :显而易见,比较有局限性,不可查询(或不易实现),较为死板,储存方式单一,难以直接实现,储存什么样的内容,拿出什么样的内容,不可加密储存,明文储存,“非主流”。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值