【Android】数据编辑界面&Sqlite数据库实现

实验4  数据存储

一、实验目的

1. 掌握使用Sqlite数据库;

2. 熟练调用API完成数据的CRUD操作;


二、实验任务

1. 根据原型图设计界面;

2. 设计实现数据列表加载展示;

3. 设计实现数据的添加、编辑和删除


三、实验内容与要求

3.1 界面设计

   (1)设计数据编辑界面

   (2)设计输入,编辑,删除等按钮,并展示运行后的数据

3.3 数据添加,编辑和删除

(1)输入数据,插入到数据库中

(2)编辑数据,保存到数据库中

(3)选择数据,从数据库中删除


四、实验效果

主界面

添加商品功能模块 

修改商品功能模块

删除商品功能模块 


五、代码实现 

数据库创建

package com.example.myapplication01.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

public class ProductSqliteHelper extends SQLiteOpenHelper {
    public ProductSqliteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="Create table if not exists t_product(" +
                "_id integer primary key autoincrement," +
                "name varchar(100) not null," +
                "price integer,"+
                "number integer)";
        sqLiteDatabase.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    }
}

增加商品 

package com.example.myapplication01;
import static com.example.myapplication01.SqliteActivity.writableDatabase;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AddProductActivity extends AppCompatActivity {
    EditText name,price,number;
    Button add,clear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        name = this.findViewById(R.id.name);
        price = this.findViewById(R.id.price);
        number = this.findViewById(R.id.number);
        add = this.findViewById(R.id.add);
        clear = this.findViewById(R.id.clear);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                String productName = name.getText().toString();
                String productPrice = price.getText().toString();
                String productNumber = number.getText().toString();
                String sql="insert into t_product(name,price,number) values(?,?,?)";
                writableDatabase.execSQL(sql,new Object[]{
                        productName,productPrice,productNumber});
                Intent intent = new Intent();
                intent.setClass(AddProductActivity.this,SqliteActivity.class);
                startActivity(intent);
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                name.setText("");
                price.setText("");
                number.setText("");
            }
        });
    }
}

删除商品

package com.example.myapplication01;

import static com.example.myapplication01.SqliteActivity.writableDatabase;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class DeleteProductActivity extends AppCompatActivity {
    EditText productId;
    Button delete,clear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        productId = this.findViewById(R.id.productId);
        delete = this.findViewById(R.id.delete);
        clear = this.findViewById(R.id.clear);

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                String id = productId.getText().toString();
                String sql="delete from t_product where _id=?";
                writableDatabase.execSQL(sql,new Object[]{
                        id});
                Intent intent = new Intent();
                intent.setClass(DeleteProductActivity.this,SqliteActivity.class);
                startActivity(intent);
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                productId.setText("");
            }
        });
    }
}

修改商品 

package com.example.myapplication01;

import static com.example.myapplication01.SqliteActivity.writableDatabase;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class UpdateProductActivity extends AppCompatActivity {
    EditText id,name,price,number;
    Button update,clear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        id=this.findViewById(R.id.productId);
        name = this.findViewById(R.id.name);
        price = this.findViewById(R.id.price);
        number = this.findViewById(R.id.number);
        update = this.findViewById(R.id.update);
        clear = this.findViewById(R.id.clear);

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                String productId = id.getText().toString();
                String productName = name.getText().toString();
                String productPrice = price.getText().toString();
                String productNumber = number.getText().toString();
                String sql="update t_product set name=?,price=?,number=? where _id=?";
                writableDatabase.execSQL(sql,new Object[]{
                        productName,productPrice,productNumber,productId});
                Intent intent = new Intent();
                intent.setClass(UpdateProductActivity.this,SqliteActivity.class);
                startActivity(intent);
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                id.setText("");
                name.setText("");
                price.setText("");
                number.setText("");
            }
        });
    }
}

实体类 

package com.example.myapplication01;
public class Product {
    private Integer _id;
    private String name;
    private Integer price;
    private Integer number;

    public Product(Integer id, String name, Integer price, Integer number) {
        this._id = id;
        this.name = name;
        this.price = price;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Product{" +
                "_id=" + _id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", number=" + number +
                '}';
    }
    public Integer get_id() {
        return _id;
    }
    public void set_id(Integer _id) {
        this._id = _id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }
    public Integer getNumber() {
        return number;
    }
    public void setNumber(Integer number) {
        this.number = number;
    }
}

六、实验小结

通过这次实验,我锻炼了sqlite的增删改查的操作技能,为我的进一步学习打下了基础。目前我已经能够运用Android Studio和sqlite设计简单的安卓app并实现其简单的增删改查功能,这些小技能无一不为日后的课程设计打下相应的基础。在学习过程中,我积极查阅了资料并请教同学完成了实验。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

米莱虾

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值