0

I want to display data from MySQL using PHP file, JSON form and volley. I retrieve data ok, however does not display it on custom list using SimpleAdapter.

This is the code I'm using

JsonObjectRequest jreq = new JsonObjectRequest(Method.GET, url,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    JSONArray ja = null;
                    try {
                        ja = response.getJSONArray("report");

                        for (int i = 0; i < ja.length(); i++) {

                            JSONObject jobj = ja.getJSONObject(i);
                            HashMap<String, String> item = new HashMap<String, String>();
                            item.put(DATA, jobj.getString(DATA));
                            item.put(VALOR, jobj.getString(VALOR));
                            item.put(KMS, jobj.getString(KMS));
                            item.put(HORAS, jobj.getString(HORAS));
                            item.put(MEDIAKMS, jobj.getString(MEDIAKMS));
                            item.put(MEDIAHORAS, jobj.getString(MEDIAHORAS));

                            Item_List.add(item);



                        } // for loop ends




                        adapter = new SimpleAdapter(
                                getApplicationContext(), Item_List,
                                R.layout.list_items, new String[] {DATA, VALOR, KMS, HORAS, MEDIAKMS, MEDIAHORAS},
                                new int[] {R.id.data_list, R.id.valor_list, R.id.kms_list, R.id.horas_list, R.id.mediakms_list,
                                R.id.mediahoras_list});

                        listview.setAdapter(adapter);

SOLVED changing

getApplicationContext(), 

TO:

MainActivity.this
  • How are you determining that the data is received correctly? Is there possibly an Exception being thrown in that `try` block? If so, what is it, exactly? – Mike M. Aug 19 '18 at 07:36
  • System.out.println(response); shows the data as I/System.out: {"report":[{"id":"1","dia":"19","mes":"8","ano":"2018","semana_ano":"34","preco_comb":"1.354","kms":"320","media_comb":"6.2","portagens":"2.8","valor_uber":"120","horas_uber":"10","media_hora":"12.00","media_km":"0.38"},{"id":"2","dia":"19","mes":"8","ano":"2018","semana_ano":"34","preco_comb":"1.354","kms":"300","media_comb":"6.2","portagens":"3.1","valor_uber":"115","horas_uber":"9.3","media_hora":"12.37","media_km":"0.38"}]} – Jose Borges Aug 19 '18 at 07:39
  • And the Exception, if there is one? – Mike M. Aug 19 '18 at 07:42
  • No Exception, although logcat shows duplicate data, don't know if there's an issue – Jose Borges Aug 19 '18 at 07:48
  • I'm not sure what you mean by duplicate data, but that's likely not the issue. Are you sure that your `ListView` is visible on-screen? Is there any visual feedback if you click it after the data loads? – Mike M. Aug 19 '18 at 07:52
  • The most stupid reason, somehow text was white on white background, differently from the preview on Android Studio of the xml file. Changed the color and showed it. But I just found out because Mike M. told to click it and I saw something showing it and then changed color. – Jose Borges Aug 19 '18 at 08:00
  • Yep, that can happen when you use the wrong `Context` for the `Adapter`. Change `getApplicationContext()` to the current `Activity`; e.g. `MainActivity.this`. – Mike M. Aug 19 '18 at 08:01
  • 1
    Mike M. thanks very much for guidance – Jose Borges Aug 19 '18 at 08:04

1 Answers1

0

try to run the code on UI thread like this

runOnUiThread(new Runnable() {
@Override
public void run() {
        listview.setAdapter(adapter);
    }
 });

to make any action on any view you need to run it on the user interface thread which is the main thread

Mohammad Rbabah
  • 456
  • 2
  • 10
  • Volley's `onResponse()` already runs on the UI thread, and it would've likely thrown an Exception otherwise. – Mike M. Aug 19 '18 at 08:11