Android Studio学生列表项目如何实现编辑保存后动态更新主列表
解决方案
你的项目通过MyApplication全局存储学生列表,只要拿到要编辑的条目索引,直接修改全局数据后通知列表刷新即可实现需求,按以下步骤修改:
步骤1:修改主页面MainActivity
你当前跳转详情页时没有传递点击条目的索引,后续无法定位要编辑/删除的目标条目,先修改两个逻辑:
- 跳转详情页时额外传递条目位置
- 重写
onResume方法,每次回到主页面自动刷新列表
// 修改displayForm方法,新增position传递 public void displayForm(int position){ Intent i = new Intent(getApplicationContext(), DisplayForm.class); Student s = myStudents.getMyStudentList().get(position); i.putExtra("name",s.getName()); i.putExtra("id",s.getId()); i.putExtra("checkbox", s.isFlag()); i.putExtra("position", position); // 新增:传递当前点击的条目索引 startActivity(i); } // 新增onResume方法,刷新列表 @Override protected void onResume() { super.onResume(); if(adapter != null){ adapter.notifyDataSetChanged(); } }
步骤2:修改详情页DisplayForm
承接上一步传递的position,跳转编辑页时继续传递该参数:
public class DisplayForm extends AppCompatActivity { TextView tv_name; TextView tv_id; CheckBox cb; ImageView img; Button btn; int currentPosition; // 新增成员变量,存储当前条目的索引 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("Student Details"); setContentView(R.layout.activity_display_form); btn = findViewById(R.id.displayform_edit); tv_name = (TextView) findViewById(R.id.displayform_et_name); tv_id = (TextView) findViewById(R.id.displayform_et_id); cb = (CheckBox) findViewById(R.id.displayform_cb); img = (ImageView) findViewById(R.id.displayform_icn); Bundle incomingIntent = getIntent().getExtras(); if (incomingIntent != null){ String name = incomingIntent.getString("name"); int id = incomingIntent.getInt("id"); boolean flag = incomingIntent.getBoolean("checkbox"); currentPosition = incomingIntent.getInt("position"); // 新增:接收主页面传过来的索引 tv_name.setText(name); tv_id.setText(Integer.toString(id)); cb.setChecked(flag); } btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editStudent(); } }); } public void editStudent() { Intent i = new Intent(getApplicationContext(), EditStudents.class); boolean flag = cb.isChecked(); String name = tv_name.getText().toString(); String id = tv_id.getText().toString(); i.putExtra("name_edit", name); i.putExtra("id_edit", id); i.putExtra("checkbox_edit",flag); i.putExtra("position", currentPosition); // 新增:把索引传递给编辑页 startActivity(i); } }
步骤3:修改编辑页EditStudents,实现保存+删除功能
先在编辑页的布局文件中新增删除按钮(id设为editStud_deleteBtn),再修改Activity代码:
public class EditStudents extends AppCompatActivity { EditText et_name; EditText et_id; CheckBox cb; Button save; Button deleteBtn; // 新增删除按钮变量 int currentPosition; // 存储当前编辑条目的索引 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("Edit Students"); setContentView(R.layout.activity_edit_students); et_name = findViewById(R.id.editStud_et_name); et_id = findViewById(R.id.editStud_et_id); cb = (CheckBox) findViewById(R.id.editStud_cb); save = findViewById(R.id.editStud_saveBtn); deleteBtn = findViewById(R.id.editStud_deleteBtn); // 绑定删除按钮 Bundle incomeInt = getIntent().getExtras(); if(incomeInt != null){ String name = incomeInt.getString("name_edit"); String id = incomeInt.getString("id_edit"); boolean flag = incomeInt.getBoolean("checkbox_edit"); currentPosition = incomeInt.getInt("position"); // 接收条目索引 et_name.setText(name); et_id.setText(id); cb.setChecked(flag); } // 保存按钮逻辑 save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 1. 读取编辑后的数据 String newName = et_name.getText().toString(); int newId = Integer.parseInt(et_id.getText().toString()); boolean newFlag = cb.isChecked(); // 2. 获取全局学生列表,修改对应条目 MyStudents myStudents = ((MyApplication) getApplication()).getMyStudents(); Student target = myStudents.getMyStudentList().get(currentPosition); target.setName(newName); target.setId(newId); target.setFlag(newFlag); // 3. 返回主页面 Intent i = new Intent(EditStudents.this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i); finish(); } }); // 删除按钮逻辑 deleteBtn.setOnClickListener(v -> { MyStudents myStudents = ((MyApplication) getApplication()).getMyStudents(); myStudents.getMyStudentList().remove(currentPosition); // 返回主页面 Intent i = new Intent(EditStudents.this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i); finish(); }); } }
如果需要同步修改学生头像,按照同样的逻辑传递pictureNumber参数修改即可。
内容的提问来源于stack exchange,提问作者dezlight
相关产品推荐
相关产品推荐

