MainActivity和Show
MainActivity
package com.example.document_system;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_add;
private Button btn_query;
private Button btn_show;
private BookDBHelper mHelper;
// @SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = BookDBHelper.getInstance(this);
mHelper.openReadLink();
mHelper.openWriteLink();
btn_add = findViewById(R.id.btn_add);
btn_query =findViewById(R.id.btn_query);
btn_show =findViewById(R.id.btn_show);
btn_add.setOnClickListener(this);
btn_query.setOnClickListener(this);
btn_show.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.btn_add) {
Intent intent = new Intent(MainActivity.this, AddBookActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else if (id == R.id.btn_show) {
Intent intent2 = new Intent(MainActivity.this, ShowBooksActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent2);
} else if (id == R.id.btn_query) {
Intent intent3 = new Intent(MainActivity.this, QueryBookActivity.class);
intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent3);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mHelper.closeLink();
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="图书管理"
android:gravity="center"
android:textSize="40sp"/>
<Button
android:id="@+id/btn_query"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="查找"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
<Button
android:id="@+id/btn_add"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="添加"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
<Button
android:id="@+id/btn_show"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="查看"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
</LinearLayout>
ShowBooksActivity
package com.example.document_system;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ShowBooksActivity extends AppCompatActivity implements View.OnClickListener {
private ListView lv_book;
private Button btn_back;
private BookDBHelper mHelper;
private BookAdapter adapter;
private List<BookInfo> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_books);
lv_book = findViewById(R.id.lv_book);
btn_back = findViewById(R.id.btn_back);
btn_back.setOnClickListener(this);
mHelper = BookDBHelper.getInstance(this);
list = new ArrayList<>();
list = mHelper.queryAllBooks();
adapter = new BookAdapter(ShowBooksActivity.this,list);
lv_book.setAdapter(adapter);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_back) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
}
activity_show_books
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".ShowBooksActivity">
<ListView
android:id="@+id/lv_book"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
增删改查
AddBookActivity
package com.example.document_system;
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 AddBookActivity extends AppCompatActivity implements View.OnClickListener {
private BookDBHelper mHelper;
private EditText ed_name;
private EditText ed_author;
private EditText ed_classification;
private EditText ed_publish;
private EditText ed_page_count;
private EditText ed_price;
private Button btn_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
ed_name = findViewById(R.id.ed_name);
ed_author = findViewById(R.id.ed_author);
ed_classification = findViewById(R.id.ed_classification);
ed_publish = findViewById(R.id.ed_publish);
ed_page_count = findViewById(R.id.ed_page_count);
ed_price = findViewById(R.id.ed_price);
btn_submit = findViewById(R.id.btn_submit);
mHelper = BookDBHelper.getInstance(this);
btn_submit.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onClick(View view) {
BookInfo info = new BookInfo();
if (view.getId() == R.id.btn_submit) {
info.author = ed_author.getText().toString();
info.name = ed_name.getText().toString();
info.classification = ed_classification.getText().toString();
info.publish = ed_publish.getText().toString();
info.price = Float.parseFloat(ed_price.getText().toString());
info.pageCount = Integer.parseInt(ed_page_count.getText().toString());
mHelper.insertInfo(info);
Intent intent = new Intent(AddBookActivity.this, ShowBooksActivity.class);
startActivity(intent);
}
}
}
activity_add_book
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".AddBookActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="添加图书"
android:gravity="center"
android:textSize="40sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书名:"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="4"
android:hint="输入书名"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="作者:"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入作者"
android:inputType="text"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="分类:"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_classification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入分类"
android:inputType="text"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="出版社:"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_publish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="请输入出版社名称"
android:inputType="text"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="页数:"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_page_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="4"
android:hint="输入页数"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="定价:"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入定价"
android:inputType="date"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="提交"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
</LinearLayout>
DeleteBookActivity
package com.example.document_system;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DeleteBookActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_delete;
private Button btn_clear;
private EditText ed_delete;
private BookDBHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_book);
mHelper = BookDBHelper.getInstance(this);
btn_delete = findViewById(R.id.btn_delete);
btn_clear = findViewById(R.id.btn_clear);
ed_delete = findViewById(R.id.ed_name_delete);
btn_delete.setOnClickListener(this);
btn_clear.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.btn_delete) {
boolean flag = mHelper.deleteBookInfoByName(ed_delete.getText().toString());
if (flag) {
Toast.makeText(this, "成功" + ed_delete.getText().toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DeleteBookActivity.this, ShowBooksActivity.class);
startActivity(intent);
}
} else if (id == R.id.btn_clear) {
mHelper.deleteAllBookInfo();
Toast.makeText(this, "清空成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DeleteBookActivity.this, ShowBooksActivity.class);
startActivity(intent);
}
}
}
activity_delete_book
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".DeleteBookActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="删除图书"
android:gravity="center"
android:textSize="40sp"/>
<EditText
android:id="@+id/ed_name_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="请输入需要删除的书名"
android:textSize="25sp"
/>
<Button
android:id="@+id/btn_delete"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="删除"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
<Button
android:id="@+id/btn_clear"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="清空"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
</LinearLayout>
UpdateBookActivity
package com.example.document_system;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateBookActivity extends AppCompatActivity implements View.OnClickListener {
private BookDBHelper mHelper;
private EditText ed_name;
private EditText ed_author;
private EditText ed_classification;
private EditText ed_publish;
private EditText ed_page_count;
private EditText ed_price;
private Button btn_update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_book);
ed_name = findViewById(R.id.ed_name);
ed_author = findViewById(R.id.ed_author);
ed_classification = findViewById(R.id.ed_classification);
ed_publish = findViewById(R.id.ed_publish);
ed_page_count = findViewById(R.id.ed_page_count);
ed_price = findViewById(R.id.ed_price);
btn_update = findViewById(R.id.btn_update);
// 创建意图
Intent intent = getIntent();
if (intent != null) {
String bookName = intent.getStringExtra("BookName");
String author = intent.getStringExtra("Author");
String classification = intent.getStringExtra("Classification");
String publish = intent.getStringExtra("Publish");
int pageCount = intent.getIntExtra("PageCount", 0); // 0 is the default value
float price = intent.getFloatExtra("Price", 0.0f); // 0.0f is the default value
ed_name.setText(bookName);
ed_author.setText(author);
ed_classification.setText(classification);
ed_publish.setText(publish);
ed_page_count.setText(String.valueOf(pageCount));
ed_price.setText(String.valueOf(price));
// 在这里使用获取到的参数进行你的操作
// 例如,更新UI或执行其他业务逻辑
}
mHelper = BookDBHelper.getInstance(this);
btn_update.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_update) {
Intent intent = new Intent(UpdateBookActivity.this, ShowBooksActivity.class);
BookInfo info = new BookInfo();
info.author = ed_author.getText().toString();
info.name = ed_name.getText().toString();
info.classification = ed_classification.getText().toString();
info.publish = ed_publish.getText().toString();
info.price = Float.parseFloat(ed_price.getText().toString());
info.pageCount = Integer.parseInt(ed_page_count.getText().toString());
mHelper.updateInfoById(info);
Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
startActivity(intent);
}
}
}
activity_update_book
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".UpdateBookActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="更新图书"
android:gravity="center"
android:textSize="40sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书名:"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="4"
android:hint="输入书名"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="作者:"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入作者"
android:inputType="text"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="分类:"
android:textColor="@color/black"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_classification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入分类"
android:inputType="text"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="出版社:"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_publish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="请输入出版社名称"
android:inputType="text"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="页数:"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_page_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="4"
android:hint="输入页数"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="定价:"
android:textSize="20sp"
android:gravity="center"/>
<EditText
android:id="@+id/ed_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入定价"
android:inputType="date"
android:layout_marginEnd="10dp"/>
</LinearLayout>
<Button
android:id="@+id/btn_update"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="更新"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
</LinearLayout>
QueryBookActivity
package com.example.document_system;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class QueryBookActivity extends AppCompatActivity implements View.OnClickListener {
private BookDBHelper mHelper;
private BookAdapter adapter;
private ListView lv_book;
private Button btn_query;
private Button btn_main;
private EditText ed_query;
private List<BookInfo> list;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query_book);
ed_query = findViewById(R.id.ed_query);
lv_book = findViewById(R.id.lv_book);
list = new ArrayList<>();
mHelper = BookDBHelper.getInstance(this);
btn_query = findViewById(R.id.btn_query);
btn_query.setOnClickListener(this);
btn_main = findViewById(R.id.btn_main);
btn_main.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.btn_query) {// 成功查询到了不止一条记录,先将之前显示的内容清空
list.clear();
// 通过输入的书名进行模糊查询匹配
List<BookInfo> infoList = mHelper.queryBookInfoByName(ed_query.getText().toString());
if (infoList.size() != 0) {
// 将查询到的内容全部添加到listView中进行展示
list.addAll(infoList);
// 创建适配器
adapter = new BookAdapter(QueryBookActivity.this, list);
// 给lv_book设置指定的适配器
lv_book.setAdapter(adapter);
// 弹出消息,反馈用户
Toast.makeText(QueryBookActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
} else {
// 没有查询到相关的数据信息
Toast.makeText(QueryBookActivity.this, "未查到相应书籍!", Toast.LENGTH_SHORT).show();
}
} else if (id == R.id.btn_main) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
}
activity_query_book
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".QueryBookActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="查询图书"
android:gravity="center"
android:textSize="40sp"/>
<EditText
android:id="@+id/ed_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="请输入需要查询的书名"
android:textSize="25sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_query"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="查询"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
<Button
android:id="@+id/btn_main"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="主目录"
android:textSize="20sp"
android:gravity="center"
android:textColor="@color/black"
app:backgroundTint="@null"
android:background="#03A9F4" />
</LinearLayout>
<ListView
android:id="@+id/lv_book"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"/>
</LinearLayout>
书籍列表及书籍信息设置
BookInfo
package com.example.document_system;
import java.util.Objects;
public class BookInfo {
public String name;
public String author;
public String classification;
public String publish;
public int pageCount;
public float price;
public BookInfo(String name, String author, String classification, String publish, int pageCount, float price) {
this.name = name;
this.author = author;
this.classification = classification;
this.publish = publish;
this.pageCount = pageCount;
this.price = price;
}
public BookInfo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BookInfo bookInfo = (BookInfo) o;
return pageCount == bookInfo.pageCount && Float.compare(bookInfo.price, price) == 0 && Objects.equals(name, bookInfo.name) && Objects.equals(author, bookInfo.author) && Objects.equals(classification, bookInfo.classification) && Objects.equals(publish, bookInfo.publish);
}
@Override
public int hashCode() {
return Objects.hash(name, author, classification, publish, pageCount, price);
}
@Override
public String toString() {
return "BookInfo{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", classification='" + classification + '\'' +
", publish='" + publish + '\'' +
", pageCount=" + pageCount +
", price=" + price +
'}';
}
}
book_item(java)
package com.example.document_system;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class book_item extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_item);
}
}
book_item(XML)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- 书名 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="书名:"
android:textSize="15sp"
android:textColor="@color/black"
android:gravity="center" />
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="《西游记》"
android:textSize="15sp"
android:textColor="@color/black" />
</LinearLayout>
<!-- 作者 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="作者:"
android:textSize="15sp"
android:textColor="@color/black"
android:gravity="center" />
<TextView
android:id="@+id/tv_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="吴承恩"
android:textSize="15sp"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="分类:"
android:textSize="15sp"
android:gravity="center"/>
<TextView
android:id="@+id/tv_classification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="长篇小说"
android:textSize="15sp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="出版社:"
android:textSize="15sp"
android:gravity="center"/>
<TextView
android:id="@+id/tv_publish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="新华出版社"
android:textSize="15sp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="页数:"
android:textSize="15sp"
android:gravity="center"/>
<TextView
android:id="@+id/tv_page_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="1188"
android:textSize="15sp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="定价:"
android:textSize="15sp"
android:gravity="center"/>
<TextView
android:id="@+id/tv_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="¥99"
android:textSize="15sp"
android:textColor="@color/black"/>
</LinearLayout>
<!-- 其他信息... -->
</LinearLayout>
</androidx.cardview.widget.CardView>
数据库操作及适配器
BookDBHelper
package com.example.document_system;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class BookDBHelper extends SQLiteOpenHelper {
private static String DB_NAME = "MyBooks"; // 数据库名称
private static String TABLE_BOOK_INFO = "table_book_info"; // 表格名称
private static int DB_VERSION = 1; // 数据库版本
private static BookDBHelper mHelper = null; // 静态实例
private static SQLiteDatabase mRDB; // 读连接
private static SQLiteDatabase mWDB; // 写链接
private Context mContext; // 上下文
public static BookDBHelper getInstance(Context context)
{
/**
* context 上下文参数
* return Helper实例
*/
if(mHelper==null)
{
mHelper = new BookDBHelper(context);
}
return mHelper;
}
private BookDBHelper(Context context)
{
/**
* 私有构造函数
*/
super(context,DB_NAME,null,DB_VERSION);
}
public SQLiteDatabase openReadLink()
{
/**
* 用于打开读连接,并返回一个读连接实例
*/
if(mRDB==null||!mRDB.isOpen()) // 是否为空或者未被打开
{
mRDB = mHelper.getReadableDatabase(); // 获取读连接
}
return mRDB;
}
// 打开数据库写连接
public SQLiteDatabase openWriteLink()
{
/**
* 用于打写读连接,并返回一个写连接实例
*/
if(mWDB==null||!mWDB.isOpen())
{
mWDB = mHelper.getWritableDatabase();
}
return mWDB;
}
// 数据库关闭
public void closeLink()
{
/**
* 用于关闭读写连接
*/
if(mRDB!=null||mRDB.isOpen()) // 关闭读链接
{
mRDB.close();
mRDB=null;
}
if(mWDB!=null||mWDB.isOpen()) // 关闭写链接
{
mWDB.close();
mWDB = null;
}
}
@Override
public void onCreate(SQLiteDatabase db) {
/**
* db:传入的数据库
* 用于数据库操作
*/
String sql = "CREATE TABLE IF NOT EXISTS "+TABLE_BOOK_INFO+
"(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
"name VARCHAR NOT NULL,"+
"author VARCHAR NOT NULL,"+
"classification VARCHAR NOT NULL,"+
"publish VARCHAR NOT NULL,"+
"page_count INTEGER NOT NULL,"+
"price FLOAT NOT NULL);";
db.execSQL(sql);
}
public void insertInfo(BookInfo info)
{
/**
* 向数据库进行插入操作
*/
try
{
ContentValues values = new ContentValues();
values.put("name",info.name);
values.put("author",info.author);
values.put("classification",info.classification);
values.put("publish",info.publish);
values.put("page_count",info.pageCount);
values.put("price",info.price);
mWDB.insert(TABLE_BOOK_INFO,null,values);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public List<BookInfo> queryAllBooks()
{
/**
* 查询所有书籍
* return list:查询到的BookInfo实例集合
*/
// 初始化列表
List<BookInfo>list = new ArrayList<>();
// 拼接sql语句
String sql = "select * from "+TABLE_BOOK_INFO;
// 游标查询信息
Cursor cursor = mRDB.rawQuery(sql,null);
// 读取查询到的记录
while(cursor.moveToNext())
{
BookInfo info = new BookInfo();
info.name = cursor.getString(1);
info.author = cursor.getString(2);
info.classification = cursor.getString(3);
info.publish = cursor.getString(4);
info.pageCount = cursor.getInt(5);
info.price = cursor.getFloat(6);
list.add(info);
}
cursor.close();
return list;
}
public void deleteAllBookInfo() // 删除表中的所有记录
{
mWDB.delete(TABLE_BOOK_INFO,"1=1",null);
}
public boolean deleteBookInfoByName(String name)
{
// 通过书名查删除本信息
int num = mWDB.delete(TABLE_BOOK_INFO,"name = ?",new String[]{name});
if(num>0) // num>0 说明有记录被删除 返回true
return true;
else
return false;
}
public void updateInfoById(BookInfo info)
{
// 通过Id进行更新
ContentValues values = new ContentValues();
values.put("name",info.name);
values.put("author",info.author);
values.put("classification",info.classification);
values.put("publish",info.publish);
values.put("page_count",info.pageCount);
values.put("price",info.price);
mWDB.update(TABLE_BOOK_INFO,values,"name=? AND author=?",new String[]{String.valueOf(info.name),String.valueOf(info.author)});
}
public List<BookInfo> queryBookInfoByName(String name) {
// 查询成功返回对应的info列表,查询失败返回空列表
Cursor cursor = mRDB.query(TABLE_BOOK_INFO, null, "name LIKE ?", new String[]{"%" + name + "%"}, null, null, null);
List<BookInfo> infoList = new ArrayList<>();
while (cursor.moveToNext()) {
BookInfo info = new BookInfo();
info.name = cursor.getString(1);
info.author = cursor.getString(2);
info.classification = cursor.getString(3);
info.publish = cursor.getString(4);
info.pageCount = cursor.getInt(5);
info.price = cursor.getFloat(6);
infoList.add(info);
}
cursor.close();
return infoList;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/**
* 用于数据库版本的更新
*/
}
}
BookAdapter
package com.example.document_system;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class BookAdapter extends BaseAdapter {
private Context mContext;
private List<BookInfo> list = new ArrayList<>();
private BookDBHelper mHelper;
public BookAdapter(Context mContext, List<BookInfo> list) {
this.mContext = mContext;
this.list = list;
this.mHelper = BookDBHelper.getInstance(mContext);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.book_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvName = convertView.findViewById(R.id.tv_name);
viewHolder.tvAuthor = convertView.findViewById(R.id.tv_author);
viewHolder.tvClassification = convertView.findViewById(R.id.tv_classification);
viewHolder.tvPublish = convertView.findViewById(R.id.tv_publish);
viewHolder.tvPageCount = convertView.findViewById(R.id.tv_page_count);
viewHolder.tvPrice = convertView.findViewById(R.id.tv_price);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
BookInfo info = list.get(position);
viewHolder.tvName.setText(info.name);
viewHolder.tvAuthor.setText(info.author);
viewHolder.tvClassification.setText(info.classification);
viewHolder.tvPublish.setText(info.publish);
viewHolder.tvPageCount.setText(String.valueOf(info.pageCount));
viewHolder.tvPrice.setText(String.format("¥"+info.price));
// 为每个item的View添加点击事件监听器
convertView.setOnClickListener(v -> {
Intent intent = new Intent(mContext, UpdateBookActivity.class);
intent.putExtra("BookName", info.name);
intent.putExtra("Author", info.author);
intent.putExtra("Classification", info.classification);
intent.putExtra("Publish", info.publish);
intent.putExtra("PageCount", info.pageCount);
intent.putExtra("Price", info.price);
// 跳转到更新界面
mContext.startActivity(intent);
});
// 长按事件
convertView.setOnLongClickListener(v -> {
showDeleteDialog(info);
return true;
});
return convertView;
}
private void showDeleteDialog(BookInfo info) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Delete Item");
builder.setMessage("你确定要删除当前图书记录?");
builder.setPositiveButton("删除", (dialog, which) -> {
// 执行删除操作,例如从数据源中移除该项
list.remove(info);
mHelper.deleteBookInfoByName(info.name);
notifyDataSetChanged();
Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {
// 取消删除操作
dialog.dismiss();
});
builder.show();
}
static class ViewHolder {
TextView tvName;
TextView tvAuthor;
TextView tvClassification;
TextView tvPublish;
TextView tvPageCount;
TextView tvPrice;
}
}