How to complete the JSON Array to List View in Android without the distribution key

advertisements

First of all, i am a newbie in android. Right now, I am building an application for school. In one of it feature, there is a function to called what schedule for the student. I have learn tutorial for this function, but still it won't give me what I want. I want parsing value from JSONArray and then show values in ListView. But it give me only one accurate data only.

Let me start from JSON Array first

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"
}]

And I will Start My Code

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);

        }

And this what I get Wrong Result

And this what I want Look for the last List View

So please help me, how to fix this Thank you, and sorry if my english not so good

Best regards.

EDIT This is My Object class

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;
}

}

And this is my adapter class

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;

}

}


Set Adapter after for loop exicution like below

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);

Hope this will solve your problem.