成员管理前端与后端交互
由于此部分后端不是我实现的,故未列出后端的实现。
1.获取学生信息
代码如下:
//从后端数据库获得所有学生信息,保存到list
private ArrayList<SampleItem> getData() {
String res = HttpClient.findStudentsByCid(c_id);
JSONArray jsonArray = JSONArray.parseArray(res);
ArrayList<SampleItem> interList = new ArrayList<>();
for (int i=0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String account = jsonObject.getString("account");
String name = jsonObject.getString("name");
interList.add(new SampleItem(name,account));
}
return interList;
}
new Thread(() -> {
list = getData();
sampleItemProvider = new SampleItemProvider(getPageData(page), getFractionAbility());
getFractionAbility().getUITaskDispatcher().asyncDispatch(() -> {//获取UI 主线
listContainer.setItemProvider(sampleItemProvider);
});
}).start();
2.添加学生
代码如下:
//在这里添加学生
add_student_btn.setClickedListener(lis->{
String id = id_input.getText();
if(id.equals(null))
return;
new Thread(() -> {
String res = HttpClient.addStudentToCourse(id,c_id);
list = getData();
getFractionAbility().getUITaskDispatcher().asyncDispatch(() -> {//获取UI 主线
sampleItemProvider = new SampleItemProvider(getPageData(page), getFractionAbility());
listContainer.setItemProvider(sampleItemProvider);
add_student_frame.setVisibility(1);
});
}).start();
3.删除学生
代码如下:
delete_btn.setClickedListener(lis->{
new Thread(() -> {
int len = (Math.min(list.size(),page*14) - (page-1)*14);
for (int i = 0; i< len ; i++ ){
Checkbox cb = (Checkbox) listContainer.getComponentAt(i).findComponentById(ResourceTable.Id_item_name);
if (cb.isChecked()){
String res = HttpClient.removeStudentFromCourse(list.get(i).getId(),c_id);
}
}
list = getData();
getFractionAbility().getUITaskDispatcher().asyncDispatch(() -> {//获取UI 主线
sampleItemProvider = new SampleItemProvider(getPageData(page), getFractionAbility());
listContainer.setItemProvider(sampleItemProvider);
add_student_frame.setVisibility(1);
});
}).start();
});
4.演示