ListView未按预期从JSON阵列/适配器值解析

时间:2014-12-09 08:23:10

标签: android json listview

我知道我在这里忽略了一些小问题 - 但我似乎无法使用我设法解析的JSON数据来填充我的列表视图 - 我已经调试了它我有所有字符串的数据 - 但由于某种原因,listView不会填充,我不确定我忽略了什么:

来源:

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

1 个答案:

答案 0 :(得分:0)

我建议你这样做。

首先:AsyncTask&#39; s onPostExecute在主线程上运行,您应该使用它。让doInBackground返回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);
    }
}

这意味着如果我没有弄错的话,AsyncTask的签名就会变成AsyncTask<Void, Void, List<String>>。实际发生的是:你在jsonarray中为每个条目提取字符串。然后将其添加到列表中。您返回列表,并将其传递到onPostExecute(List<String>)。然后,我们找到listview,创建一个ArrayAdapter<String>,并将android.R.layout.simple_list_item_1设置为每个String投影的布局,我们还将列表设置为arrayadapter应该投影的内容。

之后,我们设置适配器。

编辑:asynctask的签名应该是

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

并修复你的致命错误:

  1. 返回list,而不是null。
  2. 删除您的空doInBackground(Void...args)方法
  3. doInBackgorund(String...args)更改为doInBackground(Void...args)
  4. 或完整代码

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