在Androidstudio中设计一个简单学生选课系统并给出代码以及XML代码,在这个应用程序中有两个Activity界面,分别是对课程进行操作时的CourseActivity界面,以及对学生操作的PersonActivity界面。在CourseActivity界面可以对课程进行添加,删除以及跳转到PersonActivity功能和通过ListView将所有课程显示在页面上,在PersonActivity中可以对person表数据进行增、删、改等操作和通过ListView将所有学生信息显示在页面上,在这个
时间: 2024-02-01 19:15:08 浏览: 127
Android课设--学生选课系统-源代码+设计报告
5星 · 资源好评率100%
应用程序中,我们需要创建两个Java类和两个XML布局文件,其中一个布局文件是用于CourseActivity,另一个布局文件是用于PersonActivity。以下是详细的代码和XML文件。
CourseActivity.java:
```
public class CourseActivity extends AppCompatActivity {
private ListView courseListView;
private CourseAdapter courseAdapter;
private List<Course> courseList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
courseListView = findViewById(R.id.course_list_view);
courseList = new ArrayList<>();
courseAdapter = new CourseAdapter(this, R.layout.course_list_item, courseList);
courseListView.setAdapter(courseAdapter);
Button addCourseBtn = findViewById(R.id.add_course_btn);
addCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CourseActivity.this, AddCourseActivity.class);
startActivityForResult(intent, 1);
}
});
courseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(CourseActivity.this, PersonActivity.class);
intent.putExtra("course_name", courseList.get(i).getName());
startActivity(intent);
}
});
courseListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(CourseActivity.this);
builder.setMessage("确定要删除此课程?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int j) {
courseList.remove(i);
courseAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", null);
builder.create().show();
return true;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String courseName = data.getStringExtra("course_name");
int credit = data.getIntExtra("credit", 0);
Course course = new Course(courseName, credit);
courseList.add(course);
courseAdapter.notifyDataSetChanged();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
```
PersonActivity.java:
```
public class PersonActivity extends AppCompatActivity {
private ListView personListView;
private PersonAdapter personAdapter;
private List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
personListView = findViewById(R.id.person_list_view);
personList = new ArrayList<>();
personAdapter = new PersonAdapter(this, R.layout.person_list_item, personList);
personListView.setAdapter(personAdapter);
Button addPersonBtn = findViewById(R.id.add_person_btn);
addPersonBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(PersonActivity.this, AddPersonActivity.class);
startActivityForResult(intent, 1);
}
});
personListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(PersonActivity.this);
builder.setMessage("确定要删除此学生?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int j) {
personList.remove(i);
personAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", null);
builder.create().show();
return true;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String name = data.getStringExtra("name");
String sex = data.getStringExtra("sex");
int age = data.getIntExtra("age", 0);
Person person = new Person(name, sex, age);
personList.add(person);
personAdapter.notifyDataSetChanged();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
```
activity_course.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CourseActivity">
<Button
android:id="@+id/add_course_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加课程"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"/>
<ListView
android:id="@+id/course_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/add_course_btn"/>
</RelativeLayout>
```
course_list_item.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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/course_name_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<TextView
android:id="@+id/course_credit_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>
```
activity_person.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">
<Button
android:id="@+id/add_person_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加学生"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<ListView
android:id="@+id/person_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
person_list_item.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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/person_name_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<TextView
android:id="@+id/person_sex_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
<TextView
android:id="@+id/person_age_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>
```
CourseAdapter.java:
```
public class CourseAdapter extends ArrayAdapter<Course> {
private int resourceId;
public CourseAdapter(Context context, int resourceId, List<Course> objects) {
super(context, resourceId, objects);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Course course = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.courseNameTv = view.findViewById(R.id.course_name_tv);
viewHolder.courseCreditTv = view.findViewById(R.id.course_credit_tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.courseNameTv.setText(course.getName());
viewHolder.courseCreditTv.setText(String.valueOf(course.getCredit()));
return view;
}
class ViewHolder {
TextView courseNameTv;
TextView courseCreditTv;
}
}
```
PersonAdapter.java:
```
public class PersonAdapter extends ArrayAdapter<Person> {
private int resourceId;
public PersonAdapter(Context context, int resourceId, List<Person> objects) {
super(context, resourceId, objects);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person person = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.personNameTv = view.findViewById(R.id.person_name_tv);
viewHolder.personSexTv = view.findViewById(R.id.person_sex_tv);
viewHolder.personAgeTv = view.findViewById(R.id.person_age_tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.personNameTv.setText(person.getName());
viewHolder.personSexTv.setText(person.getSex());
viewHolder.personAgeTv.setText(String.valueOf(person.getAge()));
return view;
}
class ViewHolder {
TextView personNameTv;
TextView personSexTv;
TextView personAgeTv;
}
}
```
AddCourseActivity.java:
```
public class AddCourseActivity extends AppCompatActivity {
private EditText courseNameEt;
private EditText creditEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
courseNameEt = findViewById(R.id.course_name_et);
creditEt = findViewById(R.id.credit_et);
Button confirmBtn = findViewById(R.id.confirm_btn);
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("course_name", courseNameEt.getText().toString());
intent.putExtra("credit", Integer.parseInt(creditEt.getText().toString()));
setResult(RESULT_OK, intent);
finish();
}
});
Button cancelBtn = findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
```
AddPersonActivity.java:
```
public class AddPersonActivity extends AppCompatActivity {
private EditText nameEt;
private EditText sexEt;
private EditText ageEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
nameEt = findViewById(R.id.name_et);
sexEt = findViewById(R.id.sex_et);
ageEt = findViewById(R.id.age_et);
Button confirmBtn = findViewById(R.id.confirm_btn);
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("name", nameEt.getText().toString());
intent.putExtra("sex", sexEt.getText().toString());
intent.putExtra("age", Integer.parseInt(ageEt.getText().toString()));
setResult(RESULT_OK, intent);
finish();
}
});
Button cancelBtn = findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
```
以上是一个简单的学生选课系统的实现,其中CourseActivity界面可以对课程进行添加、删除以及跳转到PersonActivity功能,PersonActivity界面可以对person表数据进行增、删、改等操作。
阅读全文