java - JSON Paring - 如何显示二级 ListView

标签 java android json

我正在将 JSON 数据解析到 ListView 中,并在 MainActivity.java 中成功解析了第一级 JSON,我在其中显示了主要位置列表,例如:

  1. 内部位置

  2. 外部位置

现在我希望每当我点击内部位置时,然后在 SecondActivity 中它应该在列表中显示德里和 NCR,外部位置也是如此,在这种情况下,每当用户点击需要显示美国

JSON 看起来像:

{
    "all": [
        {
            "title": "Inner Locations",
            "maps": [
                {
                    "title": "Delhi",
                    "markers": [
                        {
                            "name": "Connaught Place",
                            "latitude": 28.632777800000000000,
                            "longitude": 77.219722199999980000 
                        },
                        {
                            "name": "Lajpat Nagar",
                            "latitude": 28.565617900000000000,
                            "longitude": 77.243389100000060000
                        }
                    ]
                },
                {
                    "title": "NCR",
                    "markers": [
                        {
                            "name": "Gurgaon",
                            "latitude": 28.440658300000000000,
                            "longitude": 76.987347699999990000
                        },
                        {
                            "name": "Noida",
                            "latitude": 28.570000000000000000,
                            "longitude": 77.319999999999940000
                        }
                    ]
                }
            ]
        },
        {
            "title": "Outer Locations",
            "maps": [
                {
                    "title": "United States",
                    "markers": [
                        {
                            "name": "Virgin Islands",
                            "latitude": 18.335765000000000000,
                            "longitude": -64.896335000000020000
                        },
                        {
                            "name": "Vegas",
                            "latitude": 36.114646000000000000,
                            "longitude": -115.172816000000010000
                        }
                    ]
                }
            ]
        }
    ]
}

注意:但是每当我在第一个 Activity 中点击任何 ListItem 时,在 SecondActivity 中没有获得任何列表,为什么?

MainActivity.java:-

@Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://10.0.2.2/locations.json");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("all");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrieve JSON Objects
                    map.put("title", jsonobject.getString("title"));

                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();

            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_LONG).show();
                    // TODO Auto-generated method stub                  
                    Intent sendtosecond = new Intent(MainActivity.this, SecondActivity.class);
                    // Pass all data rank
                    sendtosecond.putExtra("title", arraylist.get(position).get(MainActivity.TITLE));
                    Log.d("Tapped Item::", arraylist.get(position).get(MainActivity.TITLE));
                    startActivity(sendtosecond);
                }
            });         
        }
    }
}

SecondActivity.java:

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);

    Intent in = getIntent();
    strReceived = in.getStringExtra("title");
    Log.d("Received Data::", strReceived);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
}

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://10.0.2.2/locations.json");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("maps");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrieve JSON Objects
                map.put("title", jsonobject.getString("title"));

                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

最佳答案

试试下面的方法:-

        try
        {
            JSONObject j = new JSONObject("your json response");
            JSONArray all = j.getJSONArray("all");
            for (int i = 0; i < all.length(); i++)
            {
                JSONObject data = all.getJSONObject(i);
                System.out.println("title       =>"+data.getString("title"));
                JSONArray maps = data.getJSONArray("maps");
                for (int k = 0; k < maps.length(); k++)
                {
                    JSONObject data_sec = maps.getJSONObject(k);
                    System.out.println("name        => "+data_sec.getString("name"));
                    System.out.println("latitude    => "+data_sec.getString("latitude"));
                    System.out.println("longitude   => "+data_sec.getString("longitude"));

                }


            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }

关于java - JSON Paring - 如何显示二级 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24176719/

相关文章:

java - 有没有办法在 Android Studio 中标记 TODO 完成?

android - 在应用程序安装期间获取针对 API 26 的所有权限

java - 初始化锯齿状数组 Java

java - 不同区域的日期/时间问题

java - Akka:我应该在 Actor 中使用parallelStream还是executors

javascript - App.post req.body 对象显示为空

java - Android 解析 Json 对 Java ArrayList 的响应

java - 为什么这个数组到二维数组 boolean 值是真的?

android - 我们如何在 Google Cloud Messaging 的 Android 模拟器中找到设备 token

java - Android项目中的错误