写一个基于安卓studio的图书信息管理系统,实现增删改查
时间: 2023-07-13 19:06:02 浏览: 132
好的,以下是一个基于安卓Studio的图书信息管理系统的代码实现,包括增删改查功能:
首先,在布局文件中定义UI界面,如下所示(我这里使用了RecyclerView来展示图书信息):
```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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp"
android:paddingBottom="16dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_add_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/ic_add_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
接着,在MainActivity中定义RecyclerView的Adapter和ViewHolder,如下所示:
```java
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {
private List<Book> bookList;
private OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
static class BookViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle, tvAuthor, tvISBN;
BookViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tv_title);
tvAuthor = itemView.findViewById(R.id.tv_author);
tvISBN = itemView.findViewById(R.id.tv_isbn);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
public BookAdapter(List<Book> bookList) {
this.bookList = bookList;
}
@NonNull
@Override
public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_item, parent, false);
return new BookViewHolder(itemView, listener);
}
@Override
public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
Book book = bookList.get(position);
holder.tvTitle.setText(book.getTitle());
holder.tvAuthor.setText(book.getAuthor());
holder.tvISBN.setText(book.getISBN());
}
@Override
public int getItemCount() {
return bookList.size();
}
}
```
定义Book实体类,如下所示:
```java
public class Book {
private String title;
private String author;
private String ISBN;
public Book(String title, String author, String ISBN) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
}
```
在MainActivity中,定义一个Book列表和一个BookAdapter的实例:
```java
public class MainActivity extends AppCompatActivity {
private List<Book> bookList = new ArrayList<>();
private BookAdapter bookAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
bookAdapter = new BookAdapter(bookList);
recyclerView.setAdapter(bookAdapter);
bookAdapter.setOnItemClickListener(new BookAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
// TODO: 点击事件处理
}
});
FloatingActionButton fabAddBook = findViewById(R.id.fab_add_book);
fabAddBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 添加图书操作
}
});
}
}
```
接下来,实现图书信息的增删改查功能:
添加图书:
```java
private void addBook() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("添加图书");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_book, null);
final EditText etTitle = view.findViewById(R.id.et_title);
final EditText etAuthor = view.findViewById(R.id.et_author);
final EditText etISBN = view.findViewById(R.id.et_isbn);
builder.setView(view);
builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String title = etTitle.getText().toString();
String author = etAuthor.getText().toString();
String ISBN = etISBN.getText().toString();
Book book = new Book(title, author, ISBN);
bookList.add(book);
bookAdapter.notifyItemInserted(bookList.size() - 1);
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
```
修改图书:
```java
private void editBook(final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("修改图书");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_book, null);
final EditText etTitle = view.findViewById(R.id.et_title);
final EditText etAuthor = view.findViewById(R.id.et_author);
final EditText etISBN = view.findViewById(R.id.et_isbn);
Book book = bookList.get(position);
etTitle.setText(book.getTitle());
etAuthor.setText(book.getAuthor());
etISBN.setText(book.getISBN());
builder.setView(view);
builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String title = etTitle.getText().toString();
String author = etAuthor.getText().toString();
String ISBN = etISBN.getText().toString();
Book book = bookList.get(position);
book.setTitle(title);
book.setAuthor(author);
book.setISBN(ISBN);
bookAdapter.notifyItemChanged(position);
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
```
删除图书:
```java
private void deleteBook(final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("删除图书");
builder.setMessage("确定要删除这本图书吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
bookList.remove(position);
bookAdapter.notifyItemRemoved(position);
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
```
查询图书:
```java
private void searchBook() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("查询图书");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_search_book, null);
final EditText etKeyword = view.findViewById(R.id.et_keyword);
builder.setView(view);
builder.setPositiveButton("查询", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String keyword = etKeyword.getText().toString();
List<Book> searchResult = new ArrayList<>();
for (Book book : bookList) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)
|| book.getISBN().contains(keyword)) {
searchResult.add(book);
}
}
if (searchResult.size() > 0) {
bookList.clear();
bookList.addAll(searchResult);
bookAdapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this, "未找到匹配的图书!", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
```
最后,在MainActivity中,将增删改查操作绑定到UI界面上,如下所示:
```java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_add:
addBook();
return true;
case R.id.action_search:
searchBook();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick(int position) {
final int clickedPosition = position;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择操作");
String[] options = {"修改", "删除"};
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch (i) {
case 0:
editBook(clickedPosition);
break;
case 1:
deleteBook(clickedPosition);
break;
}
}
});
builder.show();
}
```
这样,一个基于安卓Studio的图书信息管理系统就完成了。
阅读全文