建立一个android工程,修改main.xml文件并编写程序代码,用SQLite编写一个学生信息管理应用,并实现插入、删除、更新、查找等方法。
时间: 2024-03-04 12:48:07 浏览: 19
好的,下面是一个简单的示例,用于演示如何使用SQLite实现学生信息管理应用。请按照以下步骤进行操作:
1. 创建一个Android工程,命名为"StudentManageApp"。
2. 在"res/layout"目录下创建一个名为"main.xml"的布局文件,并添加以下代码:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"/>
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄"/>
<EditText
android:id="@+id/editTextGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="性别"/>
<Button
android:id="@+id/buttonInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加"/>
<Button
android:id="@+id/buttonDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:id="@+id/buttonUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新"/>
<Button
android:id="@+id/buttonSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查找"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
```
3. 在Java代码中实现学生信息管理功能。首先,在"app/build.gradle"文件中添加以下依赖:
```
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.sqlite:sqlite:2.1.0'
}
```
然后,在MainActivity.java文件中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
private EditText editTextName, editTextAge, editTextGender;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 打开或创建数据库
db = openOrCreateDatabase("student.db", Context.MODE_PRIVATE, null);
// 创建学生表
db.execSQL("CREATE TABLE IF NOT EXISTS students (name TEXT, age INTEGER, gender TEXT)");
// 获取控件
editTextName = findViewById(R.id.editTextName);
editTextAge = findViewById(R.id.editTextAge);
editTextGender = findViewById(R.id.editTextGender);
listView = findViewById(R.id.listView);
// 设置ListView的适配器
Cursor cursor = db.rawQuery("SELECT rowid _id, * FROM students", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"name"},
new int[]{android.R.id.text1},
0);
listView.setAdapter(adapter);
// 添加按钮点击事件
Button buttonInsert = findViewById(R.id.buttonInsert);
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
int age = Integer.parseInt(editTextAge.getText().toString());
String gender = editTextGender.getText().toString();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
values.put("gender", gender);
db.insert("students", null, values);
refreshListView();
}
});
// 删除按钮点击事件
Button buttonDelete = findViewById(R.id.buttonDelete);
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
db.delete("students", "name=?", new String[]{name});
refreshListView();
}
});
// 更新按钮点击事件
Button buttonUpdate = findViewById(R.id.buttonUpdate);
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
int age = Integer.parseInt(editTextAge.getText().toString());
String gender = editTextGender.getText().toString();
ContentValues values = new ContentValues();
values.put("age", age);
values.put("gender", gender);
db.update("students", values, "name=?", new String[]{name});
refreshListView();
}
});
// 查找按钮点击事件
Button buttonSearch = findViewById(R.id.buttonSearch);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
Cursor cursor = db.rawQuery("SELECT rowid _id, * FROM students WHERE name=?", new String[]{name});
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"name"},
new int[]{android.R.id.text1},
0);
listView.setAdapter(adapter);
}
});
}
// 刷新ListView
private void refreshListView() {
Cursor cursor = db.rawQuery("SELECT rowid _id, * FROM students", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"name"},
new int[]{android.R.id.text1},
0);
listView.setAdapter(adapter);
}
}
```
在代码中,我们首先创建或打开名为"student.db"的SQLite数据库,并创建名为"students"的学生信息表。然后,获取布局文件中的控件,并为添加、删除、更新和查找按钮添加点击事件。在添加、删除和更新操作中,我们使用ContentValues对象封装学生信息,并调用相应的数据库操作方法。在查找操作中,我们使用Cursor对象获取符合条件的学生信息,并更新ListView的适配器。最后,在onCreate方法中调用refreshListView方法刷新ListView。
4. 运行程序,测试功能是否正常。
希望这个示例对您有所帮助。