我有listView
,我为每个主题显示students attendance
,我正在解析JSON
直到secondlast
。为此,在获得计数时,我确实返回了attendanceBeanList.size()-1
。见下面的代码。
我这样做了:
private List<TotalAttendance_Bean> attendanceBeanList;
public TotalAttendanceAdapter(Activity activity, List<TotalAttendance_Bean> attendanceBeanList)
{
super();
this.activity = activity;
this.attendanceBeanList = attendanceBeanList;
}
@Override
public int getCount() {
return attendanceBeanList.size()-1;
}
现在,当我要获得最后一个物体时,我被困住了。我必须得到每个学生的总出勤率。为此,我必须从JSONArray
下方获取Last Object。
这是我的JSON:
[
{
"subject_name": "English-I",
"total_classes": "2",
"total_attended": "2",
"percentage": "100"
},
{
"subject_name": "English-I",
"total_classes": "2",
"total_attended": "2",
"percentage": "100"
},
{
"subject_name": "English-I",
"total_classes": "2",
"total_attended": "2",
"percentage": "100"
},
{
"subject_name": "English-I",
"total_classes": "2",
"total_attended": "2",
"percentage": "100"
},
{
"total_attendance": 90%
}
]
这是我的XML。
答案 0 :(得分:3)
您可以在解析所有total_attendance
条记录
students
JSONArray obj = new JSONArray(yourStringResponse);
// get the last object
JSONObject tot_obj = obj.getJSONObject(obj.length()-1);
// get String from last object
String tot_str = tot_obj.optString("total_attendance");
注意:optString
会将您的数据转换为String
,否则会为string
提供空""
。
现在,您可以在Activity
的{{1}}中显示数据
TextView
如果需要,您可以将yourTextView.setText(tot_str);
传递给tot_str
Adapter
答案 1 :(得分:0)
在 android kotlin 中从 JSONArray 中获取最后一个对象
<块引用>第一种方法
val bookings = jsonObject.getJSONArray("result")
val lastObj: JSONObject = bookings.getJSONObject((bookings.length() - 1))
hAddress.text = lastObj.optString("service_location")
hDate.text = lastObj.optString("visit_date")
hTime.text = lastObj.optString("visit_time")
<块引用>
第二种方法
val bookings = jsonObject.getJSONArray("result")
for (i1 in 0 until bookings.length()) {
val c1: JSONObject = bookings.getJSONObject(i1)
hAddress.text = c1.optString("service_location")
hDate.text = c1.optString("visit_date")
hTime.text = c1.optString("visit_time")
}