实验三 Android数据存储

这篇博客详细介绍了如何在Android中实现数据存储。通过一个实验,博主演示了使用SharedPreferences进行登录信息保存,实现记住密码功能,以及利用SQLite创建数据库、建立表格并插入数据,同时展示了查询数据并在ListView中显示的实现过程。
摘要由CSDN通过智能技术生成


一、实验目的及要求

(1) 掌握Android的SharedPreferences的使用
(2) 掌握在Android中使用SQLite的方法

二、实验内容及步骤

任务:根据下述要求实现对应程序

1、 根据所给界面1完成登入功能,用户名为admin,密码为123456,如果用户名和密码不是admin和123456,则使用Toast提示“用户名或密码错误”,如果是则在checkbox选中的情况下,将用户名和密码保存,当此Activity再次启动时,将已保存的数据显示到界面中,实现记住密码功能。

在这里插入图片描述
Java代码:

package com.example.test3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editTextAccount, editTextPassword;
    private CheckBox checkBox;
    private Button buttonLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getViewId();

        SharedPreferences sharedPreferences = getSharedPreferences("dadaxu", MODE_PRIVATE);
        String username = sharedPreferences.getString("Account", "");
        String password = sharedPreferences.getString("Password", "");
        if (!username.equals("") && !password.equals("")) {
            editTextAccount.setText(username);
            editTextPassword.setText(password);
            checkBox.setChecked(true);
        }


        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = editTextAccount.getText().toString();
                String password = editTextPassword.getText().toString();
                if (account.equals("admin") && password.equals("123456")) {

                    if (checkBox.isChecked()) {
                        SharedPreferences sharedPreferences = getSharedPreferences("dadaxu", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("Account", account);
                        editor.putString("Password", password);
                        editor.apply();
                    }

                } else {
                    Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void getViewId() {
        editTextAccount = findViewById(R.id.et_account);
        editTextPassword = findViewById(R.id.et_password);
        checkBox = findViewById(R.id.checkbox);
        buttonLogin = findViewById(R.id.btn_login);
    }
}

布局XML:

<?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"
    android:padding="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Account:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Password:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remember password" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Login"
        android:textAllCaps="false" />

</LinearLayout>


2、 根据图2来实现功能,启动界面有两个按钮,当点击创建按钮,需要创建一个数据库(数据库名为姓名拼音+学号),创建完数据库的同时创建student表,并往表中插入三条数据(表结构:StuNo(主键)、StuName(姓名)、Score(成绩)、ClassName(班级))。当点击查询按钮时,如图所示,将查询结果在ListView中显示。

在这里插入图片描述
SQLiteHelper.java:

package com.example.test3_2;

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

import androidx.annotation.Nullable;

/**
 * 根据图2来实现功能,启动界面有两个按钮,
 * 当点击创建按钮,需要创建一个数据库(数据库名为姓名拼音+学号)
 * ,创建完数据库的同时创建student表,并往表中插入三条数据(表结构:StuNo(主键)、StuName(姓名)、Score(成绩)、ClassName(班级))。
 * 当点击查询按钮时,如图所示,将查询结果在ListView中显示。
 */
public class SQLiteHelper extends SQLiteOpenHelper {

    public SQLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table student ("
                + "StuNo text primary key,"
                + "StuName text,"
                + "Score text,"
                + "ClassName text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}


Java代码:

package com.example.test3_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonCreate, buttonQuery;
    private ListView listView;
    private SQLiteOpenHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getViewId();

        buttonCreate.setOnClickListener(this);
        buttonQuery.setOnClickListener(this);

        dbHelper = new SQLiteHelper(MainActivity.this, "dadaxu_1111111.db", null, 1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_create:
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();

                values.put("StuName", "张三");
                values.put("Score", "95.0");
                values.put("ClassName", "计科B15-1");
                db.insert("student", null, values);
                values.clear();

                values.put("StuName", "李四");
                values.put("Score", "90.0");
                values.put("ClassName", "计科B15-1");
                db.insert("student", null, values);
                values.clear();

                values.put("StuName", "王五");
                values.put("Score", "78.0");
                values.put("ClassName", "计科B15-2");
                db.insert("student", null, values);
                break;

            case R.id.btn_query:
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();

                Cursor cursor = db1.query("student", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    List<String> list = new ArrayList<String>();
                    list.clear();
                    do {
                        list.add("姓名:" + cursor.getString(cursor.getColumnIndex("StuName"))
                                + " " + "成绩:" + cursor.getString(cursor.getColumnIndex("Score"))
                                + " " + "班级:" + cursor.getString(cursor.getColumnIndex("ClassName")));

                    } while (cursor.moveToNext());
                    ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, list);
                    listView.setAdapter(adapter);
                }
                cursor.close();
                break;
            default:
                break;
        }
    }

    public void getViewId() {
        buttonCreate = findViewById(R.id.btn_create);
        buttonQuery = findViewById(R.id.btn_query);
        listView = findViewById(R.id.listView);
    }
}


布局XML:

<?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"
    android:padding="15dp">

    <Button
        android:id="@+id/btn_create"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="创建"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btn_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="查询"
        android:textSize="24sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" />
</LinearLayout>



三、总结

码农不易,看完啦,点个赞再走吧!

在这里插入图片描述

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿代码_QQ_3014067949

给小编价格鸡腿吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值