android:text="修改一条商品信息"
android:textSize="25sp"
/>
<Button
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询一条商品信息"
android:textSize="25sp"
/>
* 总体逻辑代码-------MainActivity
package com.hnucm.androiddatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//声明增删改查四个按钮
Button addBtn;
Button delBtn;
Button updateBtn;
Button selectBtn;
//声明驱动
MySQLiteOpenHelper mySQLiteOpenHelper;
//声明数据库
SQLiteDatabase sqLiteDatabase;
//数据对象
ContentValues contentValues;
//增删改查条件变量
String id;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载驱动
mySQLiteOpenHelper = new MySQLiteOpenHelper(MainActivity.this,
"product",null,1);
//得到数据库
sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
//初始化四个按钮
addBtn = findViewById(R.id.insert);
delBtn = findViewById(R.id.delete);
updateBtn = findViewById(R.id.update);
selectBtn = findViewById(R.id.select);
//点击四个按钮
addBtn.setOnClickListener(this);
delBtn.setOnClickListener(this);
updateBtn.setOnClickListener(this);
selectBtn.setOnClickListener(this);
}
//四个按钮的点击事件
@Override
public void onClick(View view) {
switch (view.getId()){
//增加数据
case R.id.insert:
//创建数据,使用ContentValues -> HashMap
contentValues = new ContentValues();
//自增长 主键 增加无需加入id
//contentValues.put("id",1);
contentValues.put("name","辣条");
contentValues.put("singleprice",3.50);
contentValues.put("restnum",12);
//将创建好的数据对象加入数据库中的哪一个表
sqLiteDatabase.insert("products",null,contentValues);
break;
//删除数据
case R.id.delete:
//删除条件
id = "1";
name = "辣条";
//在哪张表里,根据条件删除
sqLiteDatabase.delete("products","id = ? and name = ?",
new String[]{id,name});
break;
//修改数据
case R.id.update:
//修改条件
id = "2";
//将满足条件的数据修改
contentValues = new ContentValues();
contentValues.put("name","薯片");
//在数据库中修改
sqLiteDatabase.update("products",contentValues,"id=?",
new String[]{id});
break;
//查询所有数据
case R.id.select:
//采用cursor游标查询
Cursor cursor = sqLiteDatabase.query("products",null,null,
null,null,null,null);
//游标下一个存在,即没有到最后
while(cursor.moveToNext()){
//每一条数据取出每一列
int id = cursor.getInt(cursor.getColumnIndex("id"));
name = cursor.getString(cursor.getColumnIndex("name"));
double singleprice = cursor.getDouble(cursor.getColumnIndex("singleprice"));
int restnum = cursor.getInt(cursor.getColumnIndex("restnum"));
//打印数据
Log.i("products","id:" + id + ",name:" + name + ",singleprice:"
+ singleprice + ",restnum:" + restnum);
}
break;
}
}
}
### []( )增加数据
//创建数据,使用ContentValues -> HashMap
contentValues = new ContentValues();
//自增长 主键 增加无需加入id
//contentValues.put("id",1);
contentValues.put("name","辣条");
contentValues.put("singleprice",3.50);
contentValues.put("restnum",12);
//将创建好的数据对象加入数据库中的哪一个表
sqLiteDatabase.insert("products",null,contentValues);
### []( )删除数据
//删除条件
id = "1";
name = "辣条";
//在哪张表里,根据条件删除
sqLiteDatabase.delete("products","id = ? and name = ?",
new String[]{id,name});
### []( )修改数据
//修改条件
id = "2";
//将满足条件的数据修改
contentValues = new ContentValues();
contentValues.put("name","薯片");
//在数据库中修改
sqLiteDatabase.update("products",contentValues,"id=?",
new String[]{id});
### []( )查询数据
//采用cursor游标查询
//没有查询条件,所以查询表中所有信息
Cursor cursor = sqLiteDatabase.query("products",null,null,
null,null,null,null);
//游标下一个存在,即没有到最后
while(cursor.moveToNext()){
//每一条数据取出每一列
int id = cursor.getInt(cursor.getColumnIndex("id"));
name = cursor.getString(cursor.getColumnIndex("name"));
double singleprice = cursor.getDouble(cursor.getColumnIndex("singleprice"));
int restnum = cursor.getInt(cursor.getColumnIndex("restnum"));
//打印数据
Log.i("products","id:" + id + ",name:" + name + ",singleprice:"
+ singleprice + ",restnum:" + restnum);
}
[]( )在界面上进行增删改查
-------------------------------------------------------------------------
### []( )增加数据
* 采用RecyclerView来显示数据
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/insert" />
```
* 采用三个输入框输入新增数据的信息(由于id自增长,无需输入)
<EditText
android:id="@+id/name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:background="#ffffff"
android:textSize="23sp"
android:hint="请输入物品名字"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/singleprice"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:background="#ffffff"
android:textSize="23sp"
android:hint="请输入物品单价"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />
<EditText
android:id="@+id/restnum"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:background="#ffffff"
android:textSize="23sp"
android:hint="请输入物品剩余库存量"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/singleprice" />
* RecyclerView里面每条数据的显示样式
* item\_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ProductId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="16dp"
android:text="商品编号:"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/ProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="商品名称:"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="@+id/ProductId"
app:layout_constraintStart_toStartOf="@+id/ProductId"
app:layout_constraintTop_toBottomOf="@+id/ProductId" />
<TextView
android:id="@+id/ProductPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="商品价格:"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="@+id/ProductName"
app:layout_constraintStart_toStartOf="@+id/ProductName"
app:layout_constraintTop_toBottomOf="@+id/ProductName" />
<TextView
android:id="@+id/ProductNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="商品数量:"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="@+id/ProductPrice"
app:layout_constraintStart_toStartOf="@+id/ProductPrice"
app:layout_constraintTop_toBottomOf="@+id/ProductPrice" />
<Button
android:id="@+id/deleteBtn"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="删除数据"
app:layout_constraintBottom_toBottomOf="@+id/ProductNum"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/ProductNum" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"
app:layout_constraintTop_toBottomOf="@+id/deleteBtn"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
* 创建需要显示的数据对象
* Product.class
```
package com.hnucm.androiddatabase.model;
public class Product {
public int id;
public String name;
public double singleprice;
public int restnum;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSingleprice() {
return singleprice;
}
public void setSingleprice(double singleprice) {
this.singleprice = singleprice;
}
public int getRestnum() {
return restnum;
}
public void setRestnum(int restnum) {
this.restnum = restnum;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", singleprice=" + singleprice +
", restnum=" + restnum +
'}';
}
}
```
* 使用控件缓存,缓存适配器每条数据中需要的控件
//控件缓存器
public class MyViewHolder extends RecyclerView.ViewHolder{
//声明四个显示文本
TextView showId;
TextView showName;
TextView showPrice;
TextView showNum;
//定义删除,修改按钮
Button delBtn;
Button updateBtn;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
//初始化
showId = itemView.findViewById(R.id.ProductId);
showName = itemView.findViewById(R.id.ProductName);
showPrice = itemView.findViewById(R.id.ProductPrice);
showNum = itemView.findViewById(R.id.ProductNum);
delBtn = itemView.findViewById(R.id.deleteBtn);
}
}
* 使用适配器,使每条数据格式一样,显示在RecyclerView中
/适配器
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//绑定view,在view中加入适配器
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_list,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//控件中绑定数据
//得到当前position的数据对象
Product product = mProductList.get(position);
//对象值传入,显示在文本控件上
holder.showId.setText("商品编号:" + product.getId());
holder.showName.setText("商品名称:" + product.getName());
holder.showPrice.setText("商品价格:" + product.getSingleprice());
holder.showNum.setText("商品数量:" + product.getRestnum());
}
@Override
public int getItemCount() {
//返回总的数据数量
return mProductList.size();
}
}
* 声明增加按钮,三个输入框
//声明增加数据需要的三个输入框
EditText mEditName;
EditText mEditPrice;
EditText mEditNum;
//声明增删改查的按钮
Button mAddBtn;
* 声明显示数据的控件RecyclerView,显示的数据类型Product以及适配器
//声明并且初始化显示在界面上的数据对象
ArrayList<Product> mProductList = new ArrayList<>();
//在界面上显示数据的控件
RecyclerView recyclerView;
//声明适配器
MyAdapter myAdapter;
* 初始化控件(适配器不是控件,是一个类)
//初始化控件
mAddBtn = findViewById(R.id.insert);
mEditName = findViewById(R.id.name);
mEditPrice = findViewById(R.id.singleprice);
mEditNum = findViewById(R.id.restnum);
recyclerView = findViewById(R.id.recyclerView);
* 新建适配器,将适配器加入到显示控件RecyclerView中并且设置控件的显示样式
//适配器设置,加入到recyclerview中
myAdapter = new MyAdapter();
recyclerView.setAdapter(myAdapter);
//设置显示样式
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
* 点击增加按钮,先在数据库中增加数据,后在界面显示出来。
//增加数据
case R.id.insert:
//数据库中增加
//创建数据,使用ContentValues -> HashMap
contentValues = new ContentValues();
//自增长 主键 增加无需加入id
//contentValues.put("id",1);
contentValues.put("name", mEditName.getText().toString());
contentValues.put("singleprice", mEditPrice.getText().toString());
contentValues.put("restnum", mEditNum.getText().toString());
//将创建好的数据对象加入数据库中的哪一个表
//返回新建数据的id
long id = sqLiteDatabase.insert("products", null, contentValues);
//界面上增加
Product product = new Product();
//id为返回的id
product.id = (int) id;
product.name = mEditName.getText().toString();
product.singleprice = Double.parseDouble(mEditPrice.getText().toString());
product.restnum = Integer.parseInt(mEditNum.getText().toString());
//链表中加入新增数据
mProductList.add(product);
//显示在界面上,刷新
myAdapter.notifyDataSetChanged();
break;
### []( )删除数据
* 首先在界面上显示数据库中存在的数据
//构造数据,从数据库中查找已存在的信息
Cursor cursor = sqLiteDatabase.query("products", null, null, null,
null, null, null);
//游标遍历查询
while (cursor.moveToNext()) {
Product product = new Product();
product.id = cursor.getInt(0);
product.name = cursor.getString(1);
product.singleprice = cursor.getDouble(2);
product.restnum = cursor.getInt(3);
mProductList.add(product);
}
* 删除数据按钮存在于每条数据的显示样式的布局文件中
* 所以在缓存器中初始化
* 在适配器中设置点击事件监听器
//删除按钮监听事件
holder.delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//数据库中删除数据
//表名 删除条件id 当前这条信息的id int型加""转为String类型
sqLiteDatabase.delete("products","id=?",new String[]{product.id + ""});
//界面上删除
//list中的每一条数据类型为Product,遍历,命名为p
for(Product p : mProductList){
//如果遍历到的数据的id等于要删除的数据id
if(p.id == product.id){
//将这条数据从list中移除
mProductList.remove(p);
//跳出循环
break;
}
}
//更新适配器
myAdapter.notifyDataSetChanged();
}
});
### []( )查询数据
#### []( )根据id查询
* 修改布局,增加查询按钮,增加查询输入框
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#ffffff"
android:hint="请输入物品名字"
android:textSize="23sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/singleprice"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="#ffffff"
android:hint="请输入物品单价"
android:textSize="23sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />
<EditText
android:id="@+id/restnum"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="#ffffff"
android:hint="请输入物品剩余库存量"
android:textSize="23sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/singleprice" />
<EditText
android:id="@+id/search"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="#ffffff"
android:hint="输入要查询商品的id"
android:textSize="23sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/restnum" />
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="16dp"
android:text="增加商品"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search" />
<Button
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="查询商品"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="@+id/insert"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/insert"
app:layout_constraintTop_toTopOf="@+id/insert" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/insert" />
</androidx.constraintlayout.widget.ConstraintLayout>
* 声明控件并且初始化
//声明查询条件输入框
EditText mEditSearch;
//声明增删改查的按钮
Button mSelectBtn;
//初始化控件
mSelectBtn = findViewById(R.id.select);
mEditSearch = findViewById(R.id.search);
* 设置点击事件监听器
//点击按钮
mSelectBtn.setOnClickListener(this);
* 设置点击事件逻辑实现
//查询数据
case R.id.select:
//先清空界面上已存在的数据
//界面清空,数据库中内容不变
mProductList.clear();
//得到查询条件
String flag = mEditSearch.getText().toString();
//从数据库中查找数据,可能不止一条,id为条件必定只有一条,id主键
//可能不止一条,使用游标遍历逐条加入list显示
//表名 null 查询条件 条件参数 null null null
if (flag.equals("")) {
SelectAllInfo();
myAdapter.notifyDataSetChanged();
} else {
Cursor cursor = sqLiteDatabase.query("products", null,
"id=?", new String[]{flag}, null, null, null);
while (cursor.moveToNext()) {
Product product1 = new Product();
//根据索引,幅值
product1.id = cursor.getInt(0);
product1.name = cursor.getString(1);
product1.singleprice = cursor.getDouble(2);
product1.restnum = cursor.getInt(3);
//显示查询到的数据
mProductList.add(product1);
}
尾声
你不踏出去一步,永远不知道自己潜力有多大,千万别被这个社会套在我们身上的枷锁给捆住了,30岁我不怕,35岁我一样不怕,去做自己想做的事,为自己拼一把吧!不试试怎么知道你不行呢?
改变人生,没有什么捷径可言,这条路需要自己亲自去走一走,只有深入思考,不断反思总结,保持学习的热情,一步一步构建自己完整的知识体系,才是最终的制胜之道,也是程序员应该承担的使命。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
null
if (flag.equals("")) {
SelectAllInfo();
myAdapter.notifyDataSetChanged();
} else {
Cursor cursor = sqLiteDatabase.query("products", null,
"id=?", new String[]{flag}, null, null, null);
while (cursor.moveToNext()) {
Product product1 = new Product();
//根据索引,幅值
product1.id = cursor.getInt(0);
product1.name = cursor.getString(1);
product1.singleprice = cursor.getDouble(2);
product1.restnum = cursor.getInt(3);
//显示查询到的数据
mProductList.add(product1);
}
尾声
你不踏出去一步,永远不知道自己潜力有多大,千万别被这个社会套在我们身上的枷锁给捆住了,30岁我不怕,35岁我一样不怕,去做自己想做的事,为自己拼一把吧!不试试怎么知道你不行呢?
改变人生,没有什么捷径可言,这条路需要自己亲自去走一走,只有深入思考,不断反思总结,保持学习的热情,一步一步构建自己完整的知识体系,才是最终的制胜之道,也是程序员应该承担的使命。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-Gszyu3gE-1714431316762)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!