android写图书管理系统
时间: 2023-10-18 10:10:29 浏览: 82
android图书管理系统
首先,你需要确定你的图书管理系统需要实现哪些功能,例如添加图书、删除图书、查看图书信息、借阅图书、归还图书等等。然后你需要设计你的数据结构,例如你可以使用SQLite数据库来存储图书信息。接下来,你需要实现你的图书管理系统的用户界面,包括各个功能的布局和交互逻辑。最后,你需要编写代码来实现各个功能,例如添加图书时,将图书信息添加到数据库中;查看图书信息时,从数据库中读取图书信息并显示在界面上等等。具体的实现细节需要根据你的具体需求和设计来确定。以下是一个简单的示例代码,以帮助你开始:
1. 创建SQLite数据库
首先,你需要创建一个SQLite数据库,用来存储图书信息。你可以使用以下代码创建一个名为“library.db”的数据库,并在其中创建一个名为“books”的表来存储图书信息。
```
public class LibraryDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "library.db";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_BOOKS_TABLE =
"CREATE TABLE books (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"title TEXT, " +
"author TEXT, " +
"isbn TEXT, " +
"description TEXT, " +
"borrowed INTEGER DEFAULT 0);";
public LibraryDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOKS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 简单起见,我们不进行数据迁移
db.execSQL("DROP TABLE IF EXISTS books");
onCreate(db);
}
}
```
2. 实现添加图书功能
接下来,你需要实现添加图书的功能。你可以使用以下代码创建一个名为“AddBookActivity”的Activity,用来显示添加图书的界面,并在用户输入图书信息后将其保存到数据库中。
```
public class AddBookActivity extends AppCompatActivity {
private EditText mTitleEditText;
private EditText mAuthorEditText;
private EditText mIsbnEditText;
private EditText mDescriptionEditText;
private LibraryDatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
mTitleEditText = findViewById(R.id.title_edit_text);
mAuthorEditText = findViewById(R.id.author_edit_text);
mIsbnEditText = findViewById(R.id.isbn_edit_text);
mDescriptionEditText = findViewById(R.id.description_edit_text);
mDatabaseHelper = new LibraryDatabaseHelper(this);
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = mTitleEditText.getText().toString();
String author = mAuthorEditText.getText().toString();
String isbn = mIsbnEditText.getText().toString();
String description = mDescriptionEditText.getText().toString();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title);
values.put("author", author);
values.put("isbn", isbn);
values.put("description", description);
db.insert("books", null, values);
db.close();
Toast.makeText(AddBookActivity.this, R.string.book_added_message, Toast.LENGTH_SHORT).show();
finish();
}
});
}
@Override
protected void onDestroy() {
mDatabaseHelper.close();
super.onDestroy();
}
}
```
在这个Activity中,我们通过findViewById方法获取了四个EditText控件,分别用于输入图书的标题、作者、ISBN和描述。然后我们创建了一个LibraryDatabaseHelper对象,用来获取SQLite数据库的实例。接着我们在按钮的点击事件中获取用户输入的图书信息,并将其保存到数据库中。最后,我们使用Toast来显示一个添加成功的消息,并关闭该Activity。
3. 实现查看图书信息功能
接下来,你需要实现查看图书信息的功能。你可以使用以下代码创建一个名为“ViewBookActivity”的Activity,用来显示单个图书的详细信息。
```
public class ViewBookActivity extends AppCompatActivity {
private TextView mTitleTextView;
private TextView mAuthorTextView;
private TextView mIsbnTextView;
private TextView mDescriptionTextView;
private LibraryDatabaseHelper mDatabaseHelper;
private long mBookId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_book);
mTitleTextView = findViewById(R.id.title_text_view);
mAuthorTextView = findViewById(R.id.author_text_view);
mIsbnTextView = findViewById(R.id.isbn_text_view);
mDescriptionTextView = findViewById(R.id.description_text_view);
mDatabaseHelper = new LibraryDatabaseHelper(this);
Intent intent = getIntent();
mBookId = intent.getLongExtra("book_id", -1);
if (mBookId == -1) {
finish();
return;
}
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
String[] projection = {"title", "author", "isbn", "description"};
String selection = "_id = ?";
String[] selectionArgs = {String.valueOf(mBookId)};
Cursor cursor = db.query("books", projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
String title = cursor.getString(cursor.getColumnIndex("title"));
String author = cursor.getString(cursor.getColumnIndex("author"));
String isbn = cursor.getString(cursor.getColumnIndex("isbn"));
String description = cursor.getString(cursor.getColumnIndex("description"));
mTitleTextView.setText(title);
mAuthorTextView.setText(author);
mIsbnTextView.setText(isbn);
mDescriptionTextView.setText(description);
}
cursor.close();
db.close();
}
@Override
protected void onDestroy() {
mDatabaseHelper.close();
super.onDestroy();
}
}
```
在这个Activity中,我们通过findViewById方法获取了四个TextView控件,分别用于显示图书的标题、作者、ISBN和描述。然后我们创建了一个LibraryDatabaseHelper对象,用来获取SQLite数据库的实例。接着我们从Intent中获取了要显示的图书的ID,并使用该ID从数据库中获取图书信息。如果获取成功,则将图书信息显示在界面上。最后,我们在onDestroy方法中关闭了数据库。
4. 实现删除图书功能
接下来,你需要实现删除图书的功能。你可以使用以下代码在ViewBookActivity中添加一个删除按钮,并在用户点击该按钮后从数据库中删除该图书信息。
```
public class ViewBookActivity extends AppCompatActivity {
private TextView mTitleTextView;
private TextView mAuthorTextView;
private TextView mIsbnTextView;
private TextView mDescriptionTextView;
private LibraryDatabaseHelper mDatabaseHelper;
private long mBookId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_book);
mTitleTextView = findViewById(R.id.title_text_view);
mAuthorTextView = findViewById(R.id.author_text_view);
mIsbnTextView = findViewById(R.id.isbn_text_view);
mDescriptionTextView = findViewById(R.id.description_text_view);
mDatabaseHelper = new LibraryDatabaseHelper(this);
Intent intent = getIntent();
mBookId = intent.getLongExtra("book_id", -1);
if (mBookId == -1) {
finish();
return;
}
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
String[] projection = {"title", "author", "isbn", "description"};
String selection = "_id = ?";
String[] selectionArgs = {String.valueOf(mBookId)};
Cursor cursor = db.query("books", projection, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
String title = cursor.getString(cursor.getColumnIndex("title"));
String author = cursor.getString(cursor.getColumnIndex("author"));
String isbn = cursor.getString(cursor.getColumnIndex("isbn"));
String description = cursor.getString(cursor.getColumnIndex("description"));
mTitleTextView.setText(title);
mAuthorTextView.setText(author);
mIsbnTextView.setText(isbn);
mDescriptionTextView.setText(description);
}
cursor.close();
db.close();
Button deleteButton = findViewById(R.id.delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ViewBookActivity.this);
builder.setMessage(R.string.confirm_delete_message);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
String whereClause = "_id = ?";
String[] whereArgs = {String.valueOf(mBookId)};
db.delete("books", whereClause, whereArgs);
db.close();
Toast.makeText(ViewBookActivity.this, R.string.book_deleted_message, Toast.LENGTH_SHORT).show();
finish();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
});
}
@Override
protected void onDestroy() {
mDatabaseHelper.close();
super.onDestroy();
}
}
```
在这个Activity中,我们通过findViewById方法获取了一个Button控件,并在其点击事件中创建了一个AlertDialog来提示用户是否确认删除该图书。如果用户确认删除,则从数据库中删除该图书信息,并使用Toast来显示一个删除成功的消息。最后,我们关闭该Activity。
5. 实现查看所有图书功能
最后,你需要实现查看所有图书的功能。你可以使用以下代码创建一个名为“ViewAllBooksActivity”的Activity,用来显示所有图书的列表,并在用户点击某个图书时跳转到ViewBookActivity来显示该图书的详细信息。
```
public class ViewAllBooksActivity extends AppCompatActivity {
private ListView mBooksListView;
private LibraryDatabaseHelper mDatabaseHelper;
private SimpleCursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all_books);
mBooksListView = findViewById(R.id.books_list_view);
mDatabaseHelper = new LibraryDatabaseHelper(this);
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
String[] projection = {"_id", "title", "author"};
Cursor cursor = db.query("books", projection, null, null, null, null, null);
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor,
new String[]{"title", "author"},
new int[]{android.R.id.text1, android.R.id.text2},
0);
mBooksListView.setAdapter(mAdapter);
mBooksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ViewAllBooksActivity.this, ViewBookActivity.class);
intent.putExtra("book_id", id);
startActivity(intent);
}
});
db.close();
}
@Override
protected void onDestroy() {
mDatabaseHelper.close();
super.onDestroy();
}
}
```
在这个Activity中,我们通过findViewById方法获取了一个ListView控件,并使用SimpleCursorAdapter将数据库中所有图书的标题和作者显示在ListView中。然后我们为ListView设置了一个点击事件,当用户点击某个图书时,我们跳转到ViewBookActivity来显示该图书的详细信息。最后,我们在onDestroy方法中关闭了数据库。
阅读全文