如何在Listview Android中基于相同的密钥JSON显示数据JSON

时间:2017-11-19 13:53:09

标签: android json listview datetime

我有一个问题,我将基于同一时间在Listview android中显示我的数据。我的数据如:

我的数据..

{
"metadata": {
    "code": "200",
    "message": "OK"
},
"response": {
    "count": 10,
    "List": [
        {
            "DateLog": "2017-10-11 00:00:00.000",
            "TimeLog": "2017-10-11 07:51:02.000",
            "Status": "Masuk",
            "TerminalID": 106
        },
        {
            "DateLog": "2017-10-11 00:00:00.000",
            "TimeLog": "2017-10-11 16:52:03.000",
            "Status": "Pulang",
            "TerminalID": 106
        },
        {
            "DateLog": "2017-10-10 00:00:00.000",
            "TimeLog": "2017-10-10 07:50:18.000",
            "Status": "Masuk",
            "TerminalID": 106
        },
        {
            "DateLog": "2017-10-10 00:00:00.000",
            "TimeLog": "2017-10-10 16:03:50.000",
            "Status": "Pulang",
            "TerminalID": 102
        },
        {
            "DateLog": "2017-10-09 00:00:00.000",
            "TimeLog": "2017-10-09 08:05:47.000",
            "Status": "Masuk",
            "TerminalID": 106
        },
        {
            "DateLog": "2017-10-09 00:00:00.000",
            "TimeLog": "2017-10-09 15:59:25.000",
            "Status": "Pulang",
            "TerminalID": 102
        },
        {
            "DateLog": "2017-10-07 00:00:00.000",
            "TimeLog": "2017-10-07 07:32:15.000",
            "Status": "Masuk",
            "TerminalID": 106
        },
        {
            "DateLog": "2017-10-07 00:00:00.000",
            "TimeLog": "2017-10-07 13:48:35.000",
            "Status": "Pulang",
            "TerminalID": 102
        },
        {
            "DateLog": "2017-10-06 00:00:00.000",
            "TimeLog": "2017-10-06 07:54:22.000",
            "Status": "Masuk",
            "TerminalID": 106
        },
        {
            "DateLog": "2017-10-06 00:00:00.000",
            "TimeLog": "2017-10-06 16:13:46.000",
            "Status": "Pulang",
            "TerminalID": 102
        }
    ]
}

}

我想显示我的列表数据。那里有一个关键的dateLog,我想根据dateLog显示数据。如果dateLog相同,则在一个数组中显示dateLog中的所有数据。然后我将数组输入到ListView中。我该怎么办?任何人都可以帮我解决这个问题吗?

如果有人能提供帮助,我将非常感激..

这是我的代码:我有一个AsyncTask类来获取数据并在listview中显示。

                class TampilkanAbsensi extends AsyncTask<Void, Void, String>{

                @Override
                protected String doInBackground(Void... params) {
                    //creating request handler object
                    RequestHandler requestHandler = new RequestHandler();

                    //returing the response
                    return requestHandler.sendGetDataString(Constrans.URL_ABSENSI + getNIK +"/"+ finalFirstDate +"/"+ finalLastDate);
                }

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    //buat progressDialog
                    progressDialog = new ProgressDialog(AbsensiInformationActivity.this,
                            R.style.Theme_AppCompat_DayNight_Dialog);
                    progressDialog.setIndeterminate(true);
                    progressDialog.setMessage("Tunggu...sedang ambil data Absensi.");
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);

                        lvSearch.setVisibility(View.VISIBLE);
                        progressDialog.dismiss();
                        ArrayList<HashMap<String, String>> listAbsensi = new ArrayList<HashMap<String, String>>();

                        String count = null;

                        try {
                            JSONObject obj = new JSONObject(s);
                            JSONObject header = obj.getJSONObject("metadata");

                            if (header.getInt("code") == 200) {

                                JSONObject response = obj.getJSONObject("response");
                                JSONArray list = response.getJSONArray("List");
                                count = response.getString("count");

                                // i think the problem in this, i don't know to show the data with my case
                                for (int i = 0; i < list.length(); i++){
                                    JSONObject absensi = list.getJSONObject(i);
                                    String dateLog = absensi.getString("DateLog");
                                    String timeLog = absensi.getString("TimeLog");
                                    String status = absensi.getString("Status");

                                    String cn = String.valueOf(i+1);
                                    String terminalID = absensi.getString("TerminalID");

                                    //format date
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                                    SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
                                    SimpleDateFormat formatterTime = new SimpleDateFormat("hh:mm:ss a");

                                    //Format date
                                    Date date = sdf.parse(dateLog);
                                    dateLog = formatter.format(date);

                                    //Format time
                                    Date time = sdf.parse(timeLog);
                                    timeLog = formatterTime.format(time);

                                    HashMap<String, String> absensiInfo = new HashMap<>();

                                    String timeLogMasuk = null, timeLogKeluar = null;

                                   // i try to validate with the status but did not answer my case
                                    if (status.equals("Masuk")){
                                        timeLogMasuk = timeLog;
                                    }

                                    if (status.equals("Pulang")){
                                        timeLogKeluar = timeLog;
                                    }

                                    absensiInfo.put("dateLog", dateLog);
                                    absensiInfo.put("countNumber", cn);
                                    absensiInfo.put("timeLogMasuk", timeLogMasuk);
                                    absensiInfo.put("timeLogKeluar", timeLogKeluar);


                                    listAbsensi.add(absensiInfo );



                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        if (count.equals("0")){
                            info.setVisibility(View.VISIBLE);
                        } else {
                            ListAdapter adapter = new SimpleAdapter(
                                    AbsensiInformationActivity.this, listAbsensi, R.layout.list_item_informasi_absensi,
                                    new String[]{"countNumber", "dateLog", "timeLogMasuk", "timeLogKeluar"},
                                    new int[]{R.id.countNumber, R.id.txtTglAbsensi, R.id.txtMasuk, R.id.txtKeluar});

                            lvSearch.setAdapter(adapter);
                        }



                }
            }

            TampilkanAbsensi ta = new TampilkanAbsensi();
            ta.execute();
           }

0 个答案:

没有答案