安卓studio编写学生信息管理系统
时间: 2023-06-29 13:03:30 浏览: 167
学生信息管理系统可以分为前端和后端两个部分,前端主要负责用户界面的设计和交互,后端主要负责数据的存储和处理。在安卓Studio中,可以使用Java语言进行开发。
以下是一个简单的学生信息管理系统的代码示例,你可以根据需求进行修改和完善:
1. 前端部分:
activity_main.xml文件:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学生信息管理系统"
android:textSize="24sp"
android:gravity="center_horizontal"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
<EditText
android:id="@+id/name_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学生姓名" />
<EditText
android:id="@+id/id_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学生学号" />
<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加学生信息" />
<ListView
android:id="@+id/student_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
```
MainActivity.java文件:
```
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText nameEditText, idEditText;
private Button addButton;
private ListView studentListView;
private ArrayList<String> studentList;
private ArrayAdapter<String> studentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.name_edittext);
idEditText = findViewById(R.id.id_edittext);
addButton = findViewById(R.id.add_button);
studentListView = findViewById(R.id.student_listview);
studentList = new ArrayList<>();
studentAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, studentList);
studentListView.setAdapter(studentAdapter);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String id = idEditText.getText().toString();
if (name.isEmpty() || id.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入学生姓名和学号", Toast.LENGTH_SHORT).show();
} else {
String studentInfo = name + " " + id;
studentList.add(studentInfo);
studentAdapter.notifyDataSetChanged();
nameEditText.setText("");
idEditText.setText("");
}
}
});
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedStudent = studentList.get(position);
Toast.makeText(MainActivity.this, selectedStudent, Toast.LENGTH_SHORT).show();
}
});
}
}
```
2. 后端部分:
为了存储学生信息,我们可以使用SQLite数据库。在Android Studio中,可以通过创建一个SQLiteOpenHelper类来创建和管理数据库。
DBHelper.java文件:
```
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "student.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "student_table";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ID = "id";
private static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_NAME + " TEXT, " +
COLUMN_ID + " TEXT" +
")";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
```
Student.java文件:
```
public class Student {
private String name;
private String id;
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
```
StudentDataSource.java文件:
```
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class StudentDataSource {
private SQLiteDatabase database;
private DBHelper dbHelper;
private String[] allColumns = {DBHelper.COLUMN_NAME, DBHelper.COLUMN_ID};
public StudentDataSource(Context context) {
dbHelper = new DBHelper(context);
}
public void open() {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void addStudent(Student student) {
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME, student.getName());
values.put(DBHelper.COLUMN_ID, student.getId());
database.insert(DBHelper.TABLE_NAME, null, values);
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
Cursor cursor = database.query(DBHelper.TABLE_NAME, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Student student = new Student(cursor.getString(0), cursor.getString(1));
students.add(student);
cursor.moveToNext();
}
cursor.close();
return students;
}
}
```
使用时,可以在MainActivity中调用StudentDataSource类的方法来增加和获取学生信息。例如:
```
StudentDataSource dataSource = new StudentDataSource(this);
dataSource.open();
dataSource.addStudent(new Student(name, id));
List<Student> students = dataSource.getAllStudents();
dataSource.close();
```