不积跬步,无以至千里;不积小流,无以成江海。要沉下心来,诗和远方的路费真的很贵!
Product
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 +
'}';
}
}
MainActivity
package com.hnucm.androiddatabase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.hnucm.androiddatabase.model.Product;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText mEditName;
EditText mEditPrice;
EditText mEditNum;
EditText mEditSearch;
Button mAddBtn;
Button mSelectBtn;
MySQLiteOpenHelper mySQLiteOpenHelper;
SQLiteDatabase sqLiteDatabase;
ArrayList<Product> mProductList = new ArrayList<>();
RecyclerView recyclerView;
MyAdapter myAdapter;
@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();
mSelectBtn = findViewById(R.id.select);
mAddBtn = findViewById(R.id.insert);
mEditName = findViewById(R.id.name);
mEditPrice = findViewById(R.id.singleprice);
mEditNum = findViewById(R.id.restnum);
mEditSearch = findViewById(R.id.search);
recyclerView = findViewById(R.id.recyclerView);
mAddBtn.setOnClickListener(this);
mSelectBtn.setOnClickListener(this);
SelectAllInfo();
myAdapter = new MyAdapter();
recyclerView.setAdapter(myAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.insert:
ContentValues contentValues = new ContentValues();
contentValues.put("name", mEditName.getText().toString());
contentValues.put("singleprice", mEditPrice.getText().toString());
contentValues.put("restnum", mEditNum.getText().toString());
long id = sqLiteDatabase.insert("products", null, contentValues);
Product product = new Product();
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;
case R.id.select:
mProductList.clear();
String flag = "%" + mEditSearch.getText().toString() + "%";
if (flag.equals("")) {
SelectAllInfo();
myAdapter.notifyDataSetChanged();
} else {
Cursor cursor = sqLiteDatabase.query("products", null,
"name like ?", 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);
}
myAdapter.notifyDataSetChanged();
}
break;
}
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
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) {
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());
holder.delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabase.delete("products", "id=?", new String[]{product.id + ""});
for (Product p : mProductList) {
if (p.id == product.id) {
mProductList.remove(p);
break;
}
}
myAdapter.notifyDataSetChanged();
}
});
holder.updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog(product);
}
});
}
@Override
public int getItemCount() {
return mProductList.size();
}
}
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.delete);
updateBtn = itemView.findViewById(R.id.update);
}
}
public void SelectAllInfo() {
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);
}
}
public void showDialog(Product product){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.update);
builder.setTitle("修改商品信息");
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.update_dialog,null);
builder.setView(view1);
EditText dialogEdit_id = view1.findViewById(R.id.dialogEdit_id);
EditText dialogEdit_name = view1.findViewById(R.id.dialogEdit_name);
EditText dialogEdit_price = view1.findViewById(R.id.dialogEdit_price);
EditText dialogEdit_num = view1.findViewById(R.id.dialogEdit_num);
dialogEdit_id.setText(product.id + "");
dialogEdit_name.setText(product.name);
dialogEdit_price.setText(product.singleprice + "");
dialogEdit_num.setText(product.restnum + "");
builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ContentValues contentValues = new ContentValues();
contentValues.put("name",dialogEdit_name.getText().toString());
contentValues.put("singleprice",dialogEdit_price.getText().toString());
contentValues.put("restnum",dialogEdit_num.getText().toString());
sqLiteDatabase.update("products",contentValues,
"id=?",new String[]{dialogEdit_id.getText().toString()});
for(Product p:mProductList){
if(p.id == product.id){
p.name = dialogEdit_name.getText().toString();
p.singleprice = Double.parseDouble(dialogEdit_price.getText().toString());
p.restnum = Integer.parseInt(dialogEdit_num.getText().toString());
break;
}
}
myAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
}
MySQLiteOpenHelper
package com.hnucm.androiddatabase;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(@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 products(id integer primary key autoincrement," +
"name varchar(20),singleprice double,restnum integer) ";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
activity_main.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="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="输入要查询商品的名称"
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>
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/update"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="修改数据"
app:layout_constraintBottom_toBottomOf="@+id/ProductId"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/ProductId" />
<Button
android:id="@+id/delete"
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/delete"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
update_dialog.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="match_parent">
<TextView
android:id="@+id/dialog_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="商品编号:"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/dialogEdit_id"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="输入要修改的id"
android:enabled="false"
android:background="#ffffff"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/dialog_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dialog_id"
app:layout_constraintTop_toTopOf="@+id/dialog_id" />
<TextView
android:id="@+id/dialog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="商品名称:"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dialog_id" />
<EditText
android:id="@+id/dialogEdit_name"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="输入要修改的名称"
android:background="#ffffff"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/dialog_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dialog_name"
app:layout_constraintTop_toTopOf="@+id/dialog_name" />
<TextView
android:id="@+id/dialog_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="商品价格:"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dialog_name" />
<EditText
android:id="@+id/dialogEdit_price"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="输入要修改的价格"
android:background="#ffffff"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/dialog_price"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dialog_price"
app:layout_constraintTop_toTopOf="@+id/dialog_price" />
<TextView
android:id="@+id/dialog_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="商品数量:"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dialog_price" />
<EditText
android:id="@+id/dialogEdit_num"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="输入要修改的库存"
android:background="#ffffff"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/dialog_num"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/dialog_num"
app:layout_constraintTop_toTopOf="@+id/dialog_num" />
</androidx.constraintlayout.widget.ConstraintLayout>
|