安卓开发实现一个简单书籍管理系统的添加书籍模块和展示书籍信息模块
时间: 2024-05-14 14:19:01 浏览: 75
好的,下面我来为你介绍一下如何实现一个简单的书籍管理系统的添加书籍模块和展示书籍信息模块。
首先,我们需要创建一个数据库来存储书籍信息。我们可以使用 SQLite 数据库,因为它是 Android 中默认提供的数据库。
1. 创建数据库
在 Android 中创建 SQLite 数据库的步骤如下:
1)创建一个继承自 SQLiteOpenHelper 的类 BookDbHelper,用于创建和管理数据库:
```
public class BookDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "books.db";
private static final int DATABASE_VERSION = 1;
public BookDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_BOOKS_TABLE = "CREATE TABLE " + BookContract.BookEntry.TABLE_NAME + " ("
+ BookContract.BookEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BookContract.BookEntry.COLUMN_BOOK_TITLE + " TEXT NOT NULL, "
+ BookContract.BookEntry.COLUMN_BOOK_AUTHOR + " TEXT NOT NULL, "
+ BookContract.BookEntry.COLUMN_BOOK_PUBLISHER + " TEXT NOT NULL, "
+ BookContract.BookEntry.COLUMN_BOOK_PAGES + " INTEGER NOT NULL DEFAULT 0);";
db.execSQL(SQL_CREATE_BOOKS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL("DROP TABLE IF EXISTS " + BookContract.BookEntry.TABLE_NAME);
onCreate(db);
}
}
```
2)在 BookDbHelper 的构造函数中,指定数据库的名称和版本号。
3)在 onCreate 方法中,创建一个名为 books 的表,包含 _ID、书名、作者、出版社和页数等字段。
4)在 onUpgrade 方法中,删除旧的表并重新创建新的表。
2. 添加书籍模块
添加书籍模块主要包括两个部分:UI 和数据操作。
UI 部分包括一个表单,用于输入书籍的信息。我们可以使用 EditText、Spinner 和 Button 等控件来实现。
数据操作部分主要负责将用户输入的数据保存到数据库中。我们可以使用 ContentValues 和 SQLiteDatabase 的 insert 方法来完成。
下面是实现添加书籍的示例代码:
```
public class AddBookActivity extends AppCompatActivity {
private EditText mTitleEditText;
private EditText mAuthorEditText;
private EditText mPublisherEditText;
private EditText mPagesEditText;
private Button mSaveButton;
private BookDbHelper mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
mTitleEditText = findViewById(R.id.edit_book_title);
mAuthorEditText = findViewById(R.id.edit_book_author);
mPublisherEditText = findViewById(R.id.edit_book_publisher);
mPagesEditText = findViewById(R.id.edit_book_pages);
mSaveButton = findViewById(R.id.button_save);
mDbHelper = new BookDbHelper(this);
mSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveBook();
}
});
}
private void saveBook() {
String title = mTitleEditText.getText().toString().trim();
String author = mAuthorEditText.getText().toString().trim();
String publisher = mPublisherEditText.getText().toString().trim();
int pages = Integer.parseInt(mPagesEditText.getText().toString().trim());
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BookContract.BookEntry.COLUMN_BOOK_TITLE, title);
values.put(BookContract.BookEntry.COLUMN_BOOK_AUTHOR, author);
values.put(BookContract.BookEntry.COLUMN_BOOK_PUBLISHER, publisher);
values.put(BookContract.BookEntry.COLUMN_BOOK_PAGES, pages);
long newRowId = db.insert(BookContract.BookEntry.TABLE_NAME, null, values);
if (newRowId == -1) {
Toast.makeText(this, "Error saving book", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Book saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
finish();
}
}
}
```
3. 展示书籍信息模块
展示书籍信息模块主要包括两个部分:UI 和数据操作。
UI 部分包括一个列表,用于展示所有的书籍信息。我们可以使用 RecyclerView 和 CardView 等控件来实现。
数据操作部分主要负责从数据库中读取数据并显示在列表中。我们可以使用 Cursor 和 SimpleCursorAdapter 等类来完成。
下面是实现展示书籍信息的示例代码:
```
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private BookAdapter mAdapter;
private BookDbHelper mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mDbHelper = new BookDbHelper(this);
Cursor cursor = getAllBooks();
mAdapter = new BookAdapter(this, cursor);
mRecyclerView.setAdapter(mAdapter);
}
private Cursor getAllBooks() {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
BookContract.BookEntry._ID,
BookContract.BookEntry.COLUMN_BOOK_TITLE,
BookContract.BookEntry.COLUMN_BOOK_AUTHOR,
BookContract.BookEntry.COLUMN_BOOK_PUBLISHER,
BookContract.BookEntry.COLUMN_BOOK_PAGES};
return db.query(
BookContract.BookEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null);
}
}
```
```
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {
private Context mContext;
private Cursor mCursor;
public BookAdapter(Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
}
@NonNull
@Override
public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.book_item, parent, false);
return new BookViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
if (!mCursor.moveToPosition(position)) {
return;
}
String title = mCursor.getString(mCursor.getColumnIndex(BookContract.BookEntry.COLUMN_BOOK_TITLE));
String author = mCursor.getString(mCursor.getColumnIndex(BookContract.BookEntry.COLUMN_BOOK_AUTHOR));
String publisher = mCursor.getString(mCursor.getColumnIndex(BookContract.BookEntry.COLUMN_BOOK_PUBLISHER));
int pages = mCursor.getInt(mCursor.getColumnIndex(BookContract.BookEntry.COLUMN_BOOK_PAGES));
holder.titleTextView.setText(title);
holder.authorTextView.setText(author);
holder.publisherTextView.setText(publisher);
holder.pagesTextView.setText(String.valueOf(pages));
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public class BookViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView authorTextView;
public TextView publisherTextView;
public TextView pagesTextView;
public BookViewHolder(@NonNull View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.text_book_title);
authorTextView = itemView.findViewById(R.id.text_book_author);
publisherTextView = itemView.findViewById(R.id.text_book_publisher);
pagesTextView = itemView.findViewById(R.id.text_book_pages);
}
}
}
```
以上就是一个简单的书籍管理系统的添加书籍模块和展示书籍信息模块的实现方法。需要注意的是,此示例代码仅供参考,实际开发中还需要考虑数据的验证、用户体验等方面。
阅读全文