Android中提取JSON数组内TestID最大的学生最新考试成绩
如何获取每个学生最新考试成绩并在Android ListView展示?
我来帮你解决这个问题!要实现获取每个学生最新的考试成绩(通过TestID最大值判断),并在自定义ListView中展示,我们可以按照以下步骤来操作:
1. 先创建数据模型类(可选但推荐)
为了更清晰地处理数据,避免直接操作JSONObject的繁琐和易出错,我们可以创建两个模型类:
Student.java
public class Student { private int studentId; private StudentMark latestMark; public Student(int studentId, StudentMark latestMark) { this.studentId = studentId; this.latestMark = latestMark; } // Getter方法 public int getStudentId() { return studentId; } public StudentMark getLatestMark() { return latestMark; } }
StudentMark.java
public class StudentMark { private int english; private int maths; private int science; private int history; private int testId; private String date; public StudentMark(int english, int maths, int science, int history, int testId, String date) { this.english = english; this.maths = maths; this.science = science; this.history = history; this.testId = testId; this.date = date; } // 各科成绩的Getter方法 public int getEnglish() { return english; } public int getMaths() { return maths; } public int getScience() { return science; } public int getHistory() { return history; } public int getTestId() { return testId; } public String getDate() { return date; } }
2. 修改JSON解析逻辑,提取每个学生的最新成绩
核心思路是:遍历每个学生对象,再遍历该学生的StudentMarks数组,找到TestID最大的那一条成绩记录。
try { JSONArray studentArray = new JSONArray(jsonStr); List<Student> studentList = new ArrayList<>(); for (int i = 0; i < studentArray.length(); i++) { JSONObject studentObj = studentArray.getJSONObject(i); int studentId = studentObj.getInt("StudentID"); // 获取当前学生的所有考试成绩数组 JSONArray marksArray = studentObj.getJSONArray("StudentMarks"); JSONObject latestMarkObj = null; int maxTestId = -1; // 遍历成绩数组,找到TestID最大的记录 for (int j = 0; j < marksArray.length(); j++) { JSONObject markObj = marksArray.getJSONObject(j); int currentTestId = markObj.getInt("TestID"); if (currentTestId > maxTestId) { maxTestId = currentTestId; latestMarkObj = markObj; } } // 将找到的最新成绩转为模型类,添加到列表中 if (latestMarkObj != null) { int english = latestMarkObj.getInt("English"); int maths = latestMarkObj.getInt("Maths"); int science = latestMarkObj.getInt("Science"); int history = latestMarkObj.getInt("History"); String date = latestMarkObj.getString("Date"); StudentMark latestMark = new StudentMark(english, maths, science, history, maxTestId, date); Student student = new Student(studentId, latestMark); studentList.add(student); } } // 现在studentList里就是每个学生ID和对应的最新成绩,接下来绑定到ListView StudentAdapter adapter = new StudentAdapter(this, studentList); listView.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); // 处理JSON解析异常,比如提示用户数据加载失败 }
3. 实现自定义ListView Adapter
接下来写一个简单的自定义Adapter,把数据展示到列表项中:
StudentAdapter.java
public class StudentAdapter extends ArrayAdapter<Student> { private Context context; private List<Student> studentList; public StudentAdapter(Context context, List<Student> studentList) { super(context, 0, studentList); this.context = context; this.studentList = studentList; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item_student, parent, false); } Student student = studentList.get(position); StudentMark latestMark = student.getLatestMark(); // 绑定数据到列表项的控件 TextView tvStudentId = convertView.findViewById(R.id.tv_student_id); TextView tvEnglish = convertView.findViewById(R.id.tv_english); TextView tvMaths = convertView.findViewById(R.id.tv_maths); TextView tvScience = convertView.findViewById(R.id.tv_science); TextView tvHistory = convertView.findViewById(R.id.tv_history); TextView tvTestId = convertView.findViewById(R.id.tv_test_id); tvStudentId.setText("学生ID: " + student.getStudentId()); tvEnglish.setText("英语: " + latestMark.getEnglish()); tvMaths.setText("数学: " + latestMark.getMaths()); tvScience.setText("科学: " + latestMark.getScience()); tvHistory.setText("历史: " + latestMark.getHistory()); tvTestId.setText("最新考试ID: " + latestMark.getTestId()); return convertView; } }
4. 列表项布局(list_item_student.xml)
在res/layout下创建列表项的布局文件:
<?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="vertical" android:padding="16dp"> <TextView android:id="@+id/tv_student_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textStyle="bold"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="8dp"> <TextView android:id="@+id/tv_english" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_maths" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_science" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_history" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <TextView android:id="@+id/tv_test_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:textSize="12sp" android:textColor="@android:color/darker_gray"/> </LinearLayout>
关键点说明:
- 遍历
StudentMarks数组时,一定要把TestID转为整数再比较,避免字符串比较的错误(比如"10"和"2"的字符串比较会认为"2"更大)。 - 记得处理
JSONException,防止JSON格式错误导致崩溃。 - 使用模型类可以让代码更易读、易维护,后续如果需要扩展字段也更方便。
内容的提问来源于stack exchange,提问作者Roohi Zuwairiyah
相关产品推荐
相关产品推荐

