简体   繁体   中英

ListView isn't parsed from JSON Array / Adapter Values as expected

I know I've overlooked something minor here - but I can't seem to populate my list view using the JSON data I've managed to parse out - I've debugged it and I have data for all my strings - but for some reason the listView isn't populating and I'm not sure what I've overlooked:

Source:

@Override
        protected String doInBackground(String... arg0) {

            try {
                HttpURLConnection conn = (HttpURLConnection) new URL(url)
                .openConnection();
                InputStream is = conn.getInputStream();
                BufferedReader r = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                for (String line = r.readLine(); line != null; line = r
                        .readLine()) {
                    sb.append(line);
                }
                JSONArray jsonArray = new JSONArray(sb.toString());
                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject cO = jsonArray.getJSONObject(i);
                    String value1 = cO.getString("field1");

                    JSONObject oC = cO.getJSONObject("itema");
                    String value2 = oC.getString("field2");

                    JSONObject oA = oC.getJSONObject("itemb");
                    String value3 = oA.getString("field3");

                    final JSONAdapter jSONAdapter;
                    jSONAdapter = new JSONAdapter(Example.this, jsonArray);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            final ListView list;
                            list = (ListView) findViewById(R.id.list);
                            list.setAdapter(jSONAdapter);

                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

I suggest you do the following.

First: AsyncTask 's onPostExecute runs on the main thread, and you should use that instead. Let doInBackground return a List<String>

public class MyAsyncTask extends AsyncTask<Void,Void,List<String>> {
    private String url;

    public MyAsyncTask(String url) {
        this.url = url;
    }

    @Override
    protected List<String> doInBackground(Void... arg0) {
        List<String> result = new ArrayList<String>();
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url)
            .openConnection();
            InputStream is = conn.getInputStream();
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            for (String line = r.readLine(); line != null; line = r
                    .readLine()) {
                sb.append(line);
            }
            JSONArray jsonArray = new JSONArray(sb.toString());
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject cO = jsonArray.getJSONObject(i);
                    String value1 = cO.getString("field1");

                    JSONObject oC = cO.getJSONObject("itema");
                    String value2 = oC.getString("field2");

                    JSONObject oA = oC.getJSONObject("itemb");
                    String value3 = oA.getString("field3");

                //format how you want to output each row;
                result.add(value1 + " " + value2 + " - " + value3); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onPostExecute(List<String> list) {
        ListView lv = (ListView) findViewById(R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Example.this, android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);
    }
}

This means the signature of your AsyncTask becomes AsyncTask<Void, Void, List<String>> if i'm not mistaken. What actually happens is this: you extract the strings per entry in the jsonarray. Then you add it to your list. The you return the list, and it is passed into the onPostExecute(List<String>) . Then, we find the listview, create an ArrayAdapter<String> , and set android.R.layout.simple_list_item_1 as the layout each String is projected on, and we also set the list as the thing the arrayadapter should be projecting.

After that, we set the adapter.

EDIT: The signature of the asynctask should be

public class MyAsyncTask extends AsyncTask<Void,Void, List<String>> {
    public List<String> doInBackground(Void...args);
    public void onPostExecute(List<String> list);
}

And to fix your fatal error:

  1. return list , not null.
  2. delete your empty doInBackground(Void...args) method
  3. change doInBackgorund(String...args) to doInBackground(Void...args)

Or the full code

public class Example extends AsyncTask<Void,Void, List<String>> {
    final String TAG = "AsyncTaskParseJson.java";
    String url = "https://example.com";

    protected List<String> doInBackground(Void... arg0) {
        List<String> result = new ArrayList<String>();
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url)
            .openConnection();
            InputStream is = conn.getInputStream();
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            for (String line = r.readLine(); line != null; line = r
                    .readLine()) {
                sb.append(line);
            }
            JSONArray jsonArray = new JSONArray(sb.toString());
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject cO = jsonArray.getJSONObject(i);
                    String value1 = cO.getString("field1");

                    JSONObject oC = cO.getJSONObject("itema");
                    String value2 = oC.getString("field2");

                    JSONObject oA = oC.getJSONObject("itemb");
                    String value3 = oA.getString("field3");

                //format how you want to output each row;
                result.add(value1 + " " + value2 + " - " + value3);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    protected void onPostExecute(List<String> list) {
        ListView lv = (ListView) findViewById(R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Example.this, android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM