首先,我是android的新手。 现在,我正在为学校建立一个申请表。 在其中一个功能中,有一个功能可以为学生调用什么时间表。我已经学习了这个功能的教程,但它仍然没有给我我想要的东西。我想从JSONArray解析值,然后在ListView中显示值。但它只给我一个准确的数据。
让我先从JSON Array开始
JSON Array
[{
"jam_start": "07:30:00",
"jam_end": "08:05:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Seni Budaya Dan Keterampilan",
"nama_guru": "Vina Rosiyanti,S.Pd"
}, {
"jam_start": "08:06:00",
"jam_end": "08:40:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Bahasa Indonesia",
"nama_guru": "Vina Rosiyanti,S.Pd"
}, {
"jam_start": "08:41:00",
"jam_end": "09:15:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Bahasa Indonesia",
"nama_guru": "Vina Rosiyanti,S.Pd"
}, {
"jam_start": "11:01:00",
"jam_end": "11:35:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Komputer",
"nama_guru": "Resi Gusta Arina,S.Pd.Gr"
}, {
"jam_start": "11:36:00",
"jam_end": "12:10:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Komputer",
"nama_guru": "Resi Gusta Arina,S.Pd.Gr"
}, {
"jam_start": "13:01:00",
"jam_end": "13:35:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Ilmu Pengetahuan Alam",
"nama_guru": "Vina Rosiyanti,S.Pd"
}, {
"jam_start": "13:36:00",
"jam_end": "14:10:00",
"kegiatan": "Belajar (KBM)",
"mapel": "Ilmu Pengetahuan Alam",
"nama_guru": "Vina Rosiyanti,S.Pd"
}]
我将启动我的代码
MainActivity.java
String schedule = response.toString();
JSONArray array = new JSONArray(schedule);
String jamMulai = null;
String jamSelesai = null;
String kegiatan = null;
String mapel = null;
String namaGuru = null;
for (int i = 0; i < array.length(); i++) {
JSONObject arrayELement = array.getJSONObject(i);
mapel = arrayELement.getString("mapel");
namaGuru = arrayELement.getString("nama_guru");
kegiatan = arrayELement.getString("kegiatan");
jamMulai = arrayELement.getString("jam_start");
jamSelesai = arrayELement.getString("jam_end");
HashMap<String, String> scheduleDetail = new HashMap<String, String>();
scheduleDetail.put(mapel, mapel);
scheduleDetail.put(namaGuru, namaGuru);
scheduleDetail.put(kegiatan, kegiatan);
scheduleDetail.put(jamMulai, jamMulai);
scheduleDetail.put(jamSelesai, jamSelesai);
scheduleListDetail.add(scheduleDetail);
scheduleList = (ListView)findViewById(R.id.scheduleList);
ListAdapter listAdapter = new SimpleAdapter
(
Schedule.this, scheduleListDetail,
R.layout.schedulie_list, new String[]
{mapel, namaGuru, kegiatan, jamMulai, jamSelesai},
new int[]{R.id.txtNamaPelajaran, R.id.txtNamaGuru, R.id.txtKegiatan, R.id.txtJamMulai, R.id.txtJamSelesai}
);
scheduleList.setAdapter(listAdapter);
}
这就是我得到的 结果错误
这就是我想要的 寻找最后的列表视图
所以请帮助我,如何解决这个问题 谢谢,对不起,如果我的英语不太好
最好的问候。
EDIT 这是我的对象类
Schedule Object
public class ScheduleObject {
private String MataPelajaran;
private String NamaGuru;
private String Kegiatan;
private String JamMulai;
private String JamSelesai;
public String getMataPelajaran() {
return MataPelajaran;
}
public void setMataPelajaran(String mataPelajaran) {
MataPelajaran = mataPelajaran;
}
public String getNamaGuru() {
return NamaGuru;
}
public void setNamaGuru(String namaGuru) {
NamaGuru = namaGuru;
}
public String getKegiatan() {
return Kegiatan;
}
public void setKegiatan(String kegiatan) {
Kegiatan = kegiatan;
}
public String getJamMulai() {
return JamMulai;
}
public void setJamMulai(String jamMulai) {
JamMulai = jamMulai;
}
public String getJamSelesai() {
return JamSelesai;
}
public void setJamSelesai(String jamSelesai) {
JamSelesai = jamSelesai;
}
}
这是我的适配器类
enter code here
public class ScheduleAdapter extends ArrayAdapter<ScheduleObject> {
private final Context context;
private ArrayList<ScheduleObject> scheduleObjects;
public ScheduleAdapter(Context context, int textViewResourceId, ArrayList<ScheduleObject> scheduleObjects){
super(context, textViewResourceId, scheduleObjects);
this.context = context;
this.scheduleObjects = new ArrayList<ScheduleObject>();
this.scheduleObjects.addAll(scheduleObjects);
}
private class ViewHolder{
TextView MataPelajaran;
TextView NamaGuru;
TextView Kegiatan;
TextView JamMulai;
TextView JamSelesai;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = null;
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.schedulie_list, null);
viewHolder = new ViewHolder();
viewHolder.MataPelajaran = (TextView)convertView.findViewById(R.id.txtNamaPelajaran);
viewHolder.NamaGuru = (TextView)convertView.findViewById(R.id.txtNamaGuru);
viewHolder.Kegiatan = (TextView)convertView.findViewById(R.id.txtKegiatan);
viewHolder.JamMulai = (TextView)convertView.findViewById(R.id.txtJamMulai);
viewHolder.JamSelesai = (TextView)convertView.findViewById(R.id.txtJamSelesai);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
ScheduleObject scheduleObject = scheduleObjects.get(position);
viewHolder.MataPelajaran.setText(scheduleObject.getMataPelajaran());
viewHolder.NamaGuru.setText(scheduleObject.getNamaGuru());
viewHolder.Kegiatan.setText(scheduleObject.getKegiatan());
viewHolder.JamMulai.setText(scheduleObject.getJamMulai());
viewHolder.JamSelesai.setText(scheduleObject.getJamSelesai());
return convertView;
}
}
答案 0 :(得分:1)
设置适配器以进行循环驱动,如下所示
ArrayList<ScheduleObject> allscheduleObjects=new ArrayList<ScheduleObject>();
for (int i = 0; i < array.length(); i++) {
JSONObject arrayELement = array.getJSONObject(i);
ScheduleObject sche_object=new ScheduleObject ();
sche_object.setMataPelajaran(arrayELement.getString("mapel"));
sche_object.setNamaGuru(arrayELement.getString("nama_guru"));
sche_object.setKegiatan(arrayELement.getString("kegiatan"));
sche_object.setJamMulai(arrayELement.getString("jam_start"));
sche_object.setJamSelesai(arrayELement.getString("jam_end"));
allscheduleObjects.add(sche_object);
}
scheduleList = (ListView)findViewById(R.id.scheduleList);
ListAdapter listAdapter = new ScheduleAdapter
(
Schedule.this,
R.layout.schedulie_list,allscheduleObjects
);
scheduleList.setAdapter(listAdapter);
希望这能解决您的问题。
答案 1 :(得分:0)
在android studio中,在app的build.gradle中添加此依赖项
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.1.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.1.1'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.1'
你还需要一个POJO,看起来像这样
public class SchoolDTO{
private String jam_start;
private String jam_end;
private String kegiatan;
private String maple;
private String nama_guru;
...
your getter setter method
....
}
然后当你尝试将字符串json转换为list时,只需执行此操作
ArrayList<SchoolDTO> schoolDTOs = new ObjectMapper.readValue(response.toString(), new TypeReference<List<SchoolDTO>>(){});
然后将列表传递给listView这将节省大量代码