自学内容网 自学内容网

Android实训十 数据存储和访问

实训10  数据存储和访问

一、【实训目的】

1、 SharedPreferences存储数据;

2、 借助Java的I/O体系实现文件的存储,

3、使用Android内置的轻量级数据库SQLite存储数据;

二、【实训内容】

1、实现下图所示的界面,实现以下功能:

1)用SharedPreferences类,当点击”写入xml文件”按钮, 把“输入你想写入内容”控件中的数据写入到login.xml文件中;

2)用SharedPreferences类,当点击”读取内容”按钮, 把content.xml文件中的数据都到一个控件中;

3)用Java的I/O体系实现文件的存储,单击“写入login.txt文件”,把数据写入到login.txt文件中;

4)使用Android内置的轻量级数据库SQLite存储数据,单击“写入数据库”,把数据写入到daba.db3文件中;

具体步骤:

MainActivity.java

package com.example.gf;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private SharedPreferences contentxieru;
private SharedPreferences.Editor contentEditor;
MyDBHelper dbhelper;
SQLiteDatabase db;
private Button write, read,loginxieru,mysqlxieru;
private EditText writeText, readText;
private String fileName = "login.txt";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
write = (Button) findViewById(R.id.write);
read = (Button) findViewById(R.id.read);
writeText = (EditText) findViewById(R.id.writeText);
readText = (EditText) findViewById(R.id.readText);

dbhelper=new MyDBHelper(this, "HR_DB.db3", 1);
write.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//write(writeText.getText().toString());
contentxieru=getSharedPreferences("content", Context.MODE_PRIVATE);
contentEditor = contentxieru.edit();
contentEditor.putString("content", writeText.getText().toString());
contentEditor.commit();
}
});
read.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//readText.setText(read());
String name=contentxieru.getString("content","none");
readText.setText(name);
}
});
loginxieru.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
write(writeText.getText().toString());
}
});
mysqlxieru.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String sql="insert into";
db=dbhelper.getReadableDatabase();
db.execSQL(sql);
Toast.makeText(MainActivity.this, "成功添加一条员工信息", 3000).show();

}
});

}

public String read() {
StringBuilder sbBuilder = new StringBuilder("");
byte[] buffer = new byte[64];
int hasRead;
try {
FileInputStream fis = openFileInput(fileName);
while ((hasRead = fis.read(buffer)) != -1) {
sbBuilder.append(new String(buffer, 0, hasRead));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sbBuilder.toString();

}

public void write(String str) {

try {
FileOutputStream fos = openFileOutput(fileName, Context.MODE_APPEND);
PrintStream ps = new PrintStream(fos);
ps.print(str);
ps.close();
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
if (db != null && db.isOpen())
{
db.close();
}
}

}

MyDBHelper.java

package com.example.gf;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper {
public MyDBHelper(Context context, String name, int version) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("x");
String CreateTableSql="create table Employee("
+"_id integer primary key autoincrement,"
+"no varchar,"
+"name varchar,"
+"sex varchar)";
db.execSQL(CreateTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

}
}

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">FileText</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="hint">输入你想写入的内容</string>
    <string name="readhint">显示读取的内容</string>
    <string name="read">读取内容</string>
    <string name="write">写入xml文件</string>
    <string name="loginxiejin">写入login.txt文件</string>
    <string name="mysqlxiejin">写入数据库</string>
</resources>

Activity_main.xml

<LinearLayout 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:orientation="vertical"
    android:paddingLeft="20dp"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/writeText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/hint"
        android:textSize="20sp" />

    <Button
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/write"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/readText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:textColor="#ff0000"
        android:textSize="20sp" />

    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read"
        android:textSize="20sp" />
    
    <Button
        android:id="@+id/loginxie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/loginxiejin"
        android:textSize="20sp" />
    
    <Button
        android:id="@+id/mysqlxie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mysqlxiejin"
        android:textSize="20sp" />

</LinearLayout>

运行结果截图:

附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!  


原文地址:https://blog.csdn.net/weixin_47077788/article/details/145326942

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!