【问题标题】:parse the json data and display in listview解析json数据并在listview中显示
【发布时间】:2016-07-20 13:51:32
【问题描述】:

我已经从 json 中解析了一个数据,然后将其添加到 ArrayList 中。现在我必须在列表视图中显示它。怎么做?执行 doInBackground() 时发生错误。我如何设置适配器来查看这个。我很混乱。提出好的解决方案

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/list">
</ListView>

public class MainActivity extends ListActivity {
String url="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=11.021459,76.916332&radius=2000&types=atm&sensor=false&key=AIzaSyD7c1IID7zDCdcfpC69fC7CUqLjz50mcls";
ArrayList<HashMap<String, String>> cList;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new googleplaces().execute();
}

private class googleplaces extends AsyncTask<Void, Void, Void> {


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

        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        HashMap<String, String> parse = new HashMap<>();
        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                JSONArray jsonArray = jsonObj.getJSONArray("results");
                for (int i=0;i<jsonArray.length();i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    String name = object.optString("name").toString();
                    if (object.has("opening_hours")) {
                        if (object.has("open_now")) {
                            if (jsonArray.getJSONObject(i).getJSONObject("opening_hours").getString("open_now").equals("true")) {
                                //
                            } else {
                                //
                            }
                        }
                    } else {
                        //
                    }
                    JSONArray typesarray = object.getJSONArray("types");
                    for (i = 0; i < typesarray.length(); i++) {
                        String type = typesarray.getString(i);

                        String vicinity = object.optString("vicinity").toString();
                        Log.d("test",vicinity);

                        parse.put("name", name);
                        parse.put("type", type);
                        parse.put("vicinity", vicinity);
                        cList.add(parse);


                    }

                }



            }  catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
        }
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


    }
    }

}

【问题讨论】:

  • 能否请您详细说明您的错误?
  • 请添加您的错误以及您的 setadapter 代码在哪里
  • ListAdapter 适配器 = new SimpleAdapter( MainActivity.this, cList, R.layout.row_layout, new String[] { }, new int[] { R.id.textView, R.id.textView2, R.id.textView3 }); setListAdapter(适配器);我有另一个布局有 3 个文本视图我需要设置这个
  • 基本上如何在列表视图中显示它你能修改我的代码 -MichaelSpitsin

标签: android json listview android-asynctask


【解决方案1】:

1.制作一个空但不为空的ArrayList

2.将 ArrayList 传递给您的 Adapter

3.将 Adapter 设置为您的 ListView

4.下载数据,放入ArrayList

5.调用adapter.notifyDataSetChange()

然后 ListView 将刷新。

【讨论】:

    【解决方案2】:

    使用下面的代码解析你的 json 数据,然后在 onPostExecute() 中设置适配器:

        private class googleplaces extends AsyncTask<Void, Void, List<Result>> {
    
    
        @Override
        protected List<Result> doInBackground(Void... arg0) {
    
            //Create list to save list of result objects
            List<Result> list = new ArrayList<>();
    
            StringBuffer mBuffer = new StringBuffer();
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            try {
                HttpResponse response = client.execute(get);
                InputStream inputStream = response.getEntity().getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                while ((line = br.readLine()) != null) {
                    mBuffer.append(line);
                }
                Log.d("Response: ", "> " + mBuffer.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
            String jsonResult = mBuffer.toString();
    
            if (jsonResult != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonResult);
    
                    JSONArray jsonArray = jsonObj.getJSONArray("results");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        //Creating Result model class object to store details
                        Result result = new Result();
    
                        JSONObject object = jsonArray.getJSONObject(i);
                        String name = object.optString("name").toString();
                        if (object.has("id")) {
                            result.setId(object.getString("id"));
                        } else {
                            result.setId("");
                        }
                        if (object.has("name")) {
                            result.setName(object.getString("name"));
                        } else {
                            result.setName("");
                        }
                        if (object.has("vicinity")) {
                            result.setName(object.getString("vicinity"));
                        } else {
                            result.setVicinity("");
                        }
                        if (object.has("opening_hours")) {
                            if (object.getJSONObject("opening_hours").has("open_now")) {
                                result.setOpening_now(object.getJSONObject("opening_hours").getString("open_now"));
                            } else {
                                result.setOpening_now("");
                            }
                        }
                        list.add(result);
                    }
                    for (int i = 0; i < list.size(); i++) {
                        CustomLog.d("List = " + list.get(i));
                    }
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
    
            return list;
        }
    
        protected void onPostExecute(List<Result> result) {
            super.onPostExecute(result);
    
            //Here update result on UI
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2017-05-13
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      相关资源
      最近更新 更多