使用SqlLite数据库创建数据库library,并创建Book和Category这两个表。往Category里插入2条默认书类别数据(如:经济类, 1)。 (2)界面设计: ① 创建一个MainActivity,对应的布局为activity_main。 ② 添加一个RecyclerView,用于显示所有的图书条目信息,每个条目显示书的名字、作者、类别和价格。 ③ 添加一个“添加书目”的按钮,用于跳转进入AddBookActivity。 ④ 在AddBookActivity中,可以添加一本书的信息到数据库中。提示:AddBookActivity需要的组件包括:EditText、TextView、Button
时间: 2024-03-08 21:51:08 浏览: 181
AndroidStudio SqlLite数据库的使用
好的,这是一个关于SQLite数据库和Android界面设计的问题。下面是答案:
1. 使用SqlLite数据库创建数据库library,并创建Book和Category这两个表。往Category里插入2条默认书类别数据(如:经济类, 1)。
```sql
CREATE TABLE Category (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
CREATE TABLE Book (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
author TEXT,
category_id INTEGER,
price REAL,
FOREIGN KEY (category_id) REFERENCES Category(id)
);
INSERT INTO Category (name) VALUES ('经济类');
INSERT INTO Category (name) VALUES ('其他类');
```
2. 界面设计:
① 创建一个MainActivity,对应的布局为activity_main。
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@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.setAdapter(new BookAdapter(this));
}
}
```
activity_main.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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" />
<Button
android:id="@+id/add_book_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:text="添加书目" />
</RelativeLayout>
```
② 添加一个RecyclerView,用于显示所有的图书条目信息,每个条目显示书的名字、作者、类别和价格。
BookAdapter.java
```java
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder> {
private Context mContext;
private List<Book> mBookList;
public BookAdapter(Context context) {
mContext = context;
mBookList = new ArrayList<>();
loadBooks();
}
private void loadBooks() {
DBHelper dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("Book", null, null, null, null, null, null);
while (cursor.moveToNext()) {
Book book = new Book();
book.setName(cursor.getString(cursor.getColumnIndex("name")));
book.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
book.setPrice(cursor.getFloat(cursor.getColumnIndex("price")));
int categoryId = cursor.getInt(cursor.getColumnIndex("category_id"));
book.setCategory(getCategoryName(categoryId));
mBookList.add(book);
}
cursor.close();
dbHelper.close();
}
private String getCategoryName(int categoryId) {
String categoryName = "";
DBHelper dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("Category", null, "id = ?", new String[]{String.valueOf(categoryId)}, null, null, null);
if (cursor.moveToFirst()) {
categoryName = cursor.getString(cursor.getColumnIndex("name"));
}
cursor.close();
dbHelper.close();
return categoryName;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Book book = mBookList.get(position);
holder.mNameTextView.setText(book.getName());
holder.mAuthorTextView.setText(book.getAuthor());
holder.mCategoryTextView.setText(book.getCategory());
holder.mPriceTextView.setText(String.valueOf(book.getPrice()));
}
@Override
public int getItemCount() {
return mBookList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView mNameTextView;
TextView mAuthorTextView;
TextView mCategoryTextView;
TextView mPriceTextView;
ViewHolder(View itemView) {
super(itemView);
mNameTextView = itemView.findViewById(R.id.name_text_view);
mAuthorTextView = itemView.findViewById(R.id.author_text_view);
mCategoryTextView = itemView.findViewById(R.id.category_text_view);
mPriceTextView = itemView.findViewById(R.id.price_text_view);
}
}
}
```
item_book.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Book Name" />
<TextView
android:id="@+id/author_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Author" />
<TextView
android:id="@+id/category_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Category" />
<TextView
android:id="@+id/price_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Price" />
</LinearLayout>
```
③ 添加一个“添加书目”的按钮,用于跳转进入AddBookActivity。
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@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.setAdapter(new BookAdapter(this));
Button addBookButton = findViewById(R.id.add_book_button);
addBookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddBookActivity.class);
startActivity(intent);
}
});
}
}
```
activity_main.xml
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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" />
<Button
android:id="@+id/add_book_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:text="添加书目" />
</RelativeLayout>
```
④ 在AddBookActivity中,可以添加一本书的信息到数据库中。提示:AddBookActivity需要的组件包括:EditText、TextView、Button
AddBookActivity.java
```java
public class AddBookActivity extends AppCompatActivity {
private EditText mNameEditText;
private EditText mAuthorEditText;
private Spinner mCategorySpinner;
private EditText mPriceEditText;
private List<Category> mCategoryList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
mNameEditText = findViewById(R.id.name_edit_text);
mAuthorEditText = findViewById(R.id.author_edit_text);
mCategorySpinner = findViewById(R.id.category_spinner);
mPriceEditText = findViewById(R.id.price_edit_text);
loadCategories();
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = mNameEditText.getText().toString().trim();
String author = mAuthorEditText.getText().toString().trim();
Category category = mCategoryList.get(mCategorySpinner.getSelectedItemPosition());
float price = Float.parseFloat(mPriceEditText.getText().toString().trim());
if (TextUtils.isEmpty(name)) {
Toast.makeText(AddBookActivity.this, "请输入书名", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(author)) {
Toast.makeText(AddBookActivity.this, "请输入作者", Toast.LENGTH_SHORT).show();
return;
}
if (price <= 0) {
Toast.makeText(AddBookActivity.this, "请输入价格", Toast.LENGTH_SHORT).show();
return;
}
DBHelper dbHelper = new DBHelper(AddBookActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("author", author);
values.put("category_id", category.getId());
values.put("price", price);
db.insert("Book", null, values);
dbHelper.close();
finish();
}
});
}
private void loadCategories() {
mCategoryList = new ArrayList<>();
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("Category", null, null, null, null, null, null);
while (cursor.moveToNext()) {
Category category = new Category();
category.setId(cursor.getInt(cursor.getColumnIndex("id")));
category.setName(cursor.getString(cursor.getColumnIndex("name")));
mCategoryList.add(category);
}
cursor.close();
dbHelper.close();
ArrayAdapter<Category> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mCategoryList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCategorySpinner.setAdapter(adapter);
}
}
```
activity_add_book.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="书名" />
<EditText
android:id="@+id/author_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="作者" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="类别" />
<Spinner
android:id="@+id/category_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/price_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="价格"
android:inputType="numberDecimal" />
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
</LinearLayout>
```
Category.java
```java
public class Category {
private int mId;
private String mName;
public int getId() {
return mId;
}
public void setId(int id) {
mId = id;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
@NonNull
@Override
public String toString() {
return mName;
}
}
```
以上是关于SQLite数据库和Android界面设计的答案,希望能够帮到你!
阅读全文