在Androidstudio中设计一个学生管理功能界面并给出代码,在这个应用程序中有对学生操作的PersonActivity界面。在PersonActivity中可以显示所有学生信息并且对person表数据进行增、删、改等操作,在这个工程中有一个Person.java类定义了学生的属性信息,person类的定义为:id(学生号),myname(学生名),myclass(学生班级),定义了student表,通过定义DBOpenHelper类继承SQLiteOpenHelper类来创建数据库,使用SQLite存储数据
时间: 2023-12-14 11:40:05 浏览: 74
首先,我们需要在Android Studio中创建一个新的项目,并在XML布局文件中设计学生管理界面。这里我创建了一个包含两个按钮和一个列表视图的布局文件activity_person.xml,代码如下:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_person"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PersonActivity">
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:onClick="addStudent"/>
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:layout_alignTop="@+id/add_button"
android:layout_toRightOf="@+id/add_button"
android:layout_toEndOf="@+id/add_button"
android:onClick="deleteStudent"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/add_button"
android:layout_marginTop="8dp"
android:dividerHeight="1dp" />
</RelativeLayout>
```
接下来,我们需要创建一个Person类来定义学生的属性信息。这里我创建了一个Person.java类,代码如下:
```java
public class Person {
private int id;
private String myname;
private String myclass;
public Person(int id, String myname, String myclass) {
this.id = id;
this.myname = myname;
this.myclass = myclass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMyname() {
return myname;
}
public void setMyname(String myname) {
this.myname = myname;
}
public String getMyclass() {
return myclass;
}
public void setMyclass(String myclass) {
this.myclass = myclass;
}
}
```
然后,我们需要创建一个DBOpenHelper类继承SQLiteOpenHelper类来创建数据库。这里我创建了一个DBOpenHelper.java类,代码如下:
```java
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "student.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "person";
public static final String ID = "_id";
public static final String NAME = "myname";
public static final String CLASS = "myclass";
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT, " +
CLASS + " TEXT);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
```
最后,我们需要在PersonActivity中实现对学生操作的代码。这里我创建了一个PersonActivity.java类,并实现了添加、删除、修改、查询学生信息的功能,代码如下:
```java
public class PersonActivity extends AppCompatActivity {
private Button addButton, deleteButton;
private ListView listView;
private ArrayList<Person> personList;
private ArrayAdapter<Person> adapter;
private DBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
addButton = (Button) findViewById(R.id.add_button);
deleteButton = (Button) findViewById(R.id.delete_button);
listView = (ListView) findViewById(R.id.listView);
personList = new ArrayList<Person>();
adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, personList);
listView.setAdapter(adapter);
dbOpenHelper = new DBOpenHelper(this);
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = db.query(DBOpenHelper.TABLE_NAME, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(DBOpenHelper.ID));
String name = cursor.getString(cursor.getColumnIndex(DBOpenHelper.NAME));
String clazz = cursor.getString(cursor.getColumnIndex(DBOpenHelper.CLASS));
Person person = new Person(id, name, clazz);
personList.add(person);
}
cursor.close();
adapter.notifyDataSetChanged();
}
public void addStudent(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add, null);
builder.setView(dialogView);
final EditText nameEditText = (EditText) dialogView.findViewById(R.id.name_editText);
final EditText classEditText = (EditText) dialogView.findViewById(R.id.class_editText);
builder.setTitle("Add Student");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEditText.getText().toString();
String clazz = classEditText.getText().toString();
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBOpenHelper.NAME, name);
values.put(DBOpenHelper.CLASS, clazz);
long id = db.insert(DBOpenHelper.TABLE_NAME, null, values);
Person person = new Person((int)id, name, clazz);
personList.add(person);
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
public void deleteStudent(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Student");
builder.setMessage("Are you sure to delete this student?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int position = listView.getCheckedItemPosition();
if (position != -1) {
Person person = personList.get(position);
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete(DBOpenHelper.TABLE_NAME, DBOpenHelper.ID + "=?", new String[]{String.valueOf(person.getId())});
personList.remove(position);
adapter.notifyDataSetChanged();
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
public void updateStudent(View view) {
int position = listView.getCheckedItemPosition();
if (position != -1) {
Person person = personList.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add, null);
builder.setView(dialogView);
final EditText nameEditText = (EditText) dialogView.findViewById(R.id.name_editText);
final EditText classEditText = (EditText) dialogView.findViewById(R.id.class_editText);
nameEditText.setText(person.getMyname());
classEditText.setText(person.getMyclass());
builder.setTitle("Update Student");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEditText.getText().toString();
String clazz = classEditText.getText().toString();
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBOpenHelper.NAME, name);
values.put(DBOpenHelper.CLASS, clazz);
db.update(DBOpenHelper.TABLE_NAME, values, DBOpenHelper.ID + "=?", new String[]{String.valueOf(person.getId())});
person.setMyname(name);
person.setMyclass(clazz);
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
public void queryStudent(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Query Student");
builder.setMessage("Please enter the student name:");
final EditText editText = new EditText(this);
builder.setView(editText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = editText.getText().toString();
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = db.query(DBOpenHelper.TABLE_NAME, null, DBOpenHelper.NAME + "=?", new String[]{name}, null, null, null);
personList.clear();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(DBOpenHelper.ID));
String name1 = cursor.getString(cursor.getColumnIndex(DBOpenHelper.NAME));
String clazz = cursor.getString(cursor.getColumnIndex(DBOpenHelper.CLASS));
Person person = new Person(id, name1, clazz);
personList.add(person);
}
cursor.close();
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
```
在这个PersonActivity中,我们实现了添加、删除、修改、查询学生信息的功能。当我们点击添加按钮时,会弹出一个对话框,让用户输入学生的姓名和班级,并将学生信息添加到数据库中。当我们点击删除按钮时,会弹出一个对话框,让用户确认是否删除选中的学生信息,并从数据库中删除选中的学生信息。当我们点击更新按钮时,会弹出一个对话框,让用户修改选中的学生信息,并更新数据库和列表视图。当我们点击查询按钮时,会弹出一个对话框,让用户输入要查询的学生姓名,并从数据库中查询符合条件的学生信息,并更新列表视图。
阅读全文