Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON配对-如何显示二级ListView_Java_Android_Json - Fatal编程技术网

Java JSON配对-如何显示二级ListView

Java JSON配对-如何显示二级ListView,java,android,json,Java,Android,Json,我正在将JSON数据解析到ListView中,并在MainActivity.java中成功解析了JSON的第一级,其中显示了主要位置的列表,如: 内部位置 外部位置 现在我想无论何时点击内部位置,然后在第二个活动中,它应该在列表中显示德里和NCR,外部位置也是如此,在这种情况下,无论何时用户点击需要显示美国 JSON看起来像: { "all": [ { "title": "Inner Locations", "maps":

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

  • 内部位置

  • 外部位置

  • 现在我想无论何时点击内部位置,然后在第二个活动中,它应该在列表中显示德里和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
                            }
                        ]
                    }
                ]
            }
        ]
    }
    
    注意:但是每当我在第一个活动中点击任何列表项,而在第二个活动中却没有得到任何列表,为什么

    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);
                    }
                });         
            }
        }
    }
    
    @覆盖
    受保护的Void doInBackground(Void…参数){
    //创建一个数组
    arraylist=新的arraylist();
    //从给定的URL地址检索JSON对象
    jsonobject=JSONfunctions
    .getJSONfromURL(“http://10.0.2.2/locations.json");
    试一试{
    //在JSON中找到数组名称
    jsonarray=jsonobject.getJSONArray(“全部”);
    for(int i=0;i
    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;
        }
    
    @覆盖
    创建时的公共void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //从listview_main.xml获取视图
    setContentView(R.layout.listview_main);
    Intent in=getIntent();
    strReceived=in.getStringExtra(“标题”);
    Log.d(“收到的数据::”,strReceived);
    //执行下载JSON异步任务
    新建下载JSON().execute();
    }
    //下载JSON异步任务
    私有类下载JSON扩展异步任务{
    @凌驾
    受保护的void onPreExecute(){
    super.onPreExecute();
    }
    @凌驾
    受保护的Void doInBackground(Void…参数){
    //创建一个数组
    arraylist=新的arraylist();
    //从给定的URL地址检索JSON对象
    jsonobject=JSONfunctions
    .getJSONfromURL(“http://10.0.2.2/locations.json");
    试一试{
    //在JSON中找到数组名称
    jsonarray=jsonobject.getJSONArray(“映射”);
    for(int i=0;i
    //试试这种方法,希望这能帮助您解决问题。
    @凌驾
    受保护的Void doInBackground(Void…参数){
    //创建一个数组
    arraylist=新的arraylist();
    //从给定的URL地址检索JSON对象
    jsonobject=JSONfunctions
    .getJSONfromURL(“http://10.0.2.2/locations.json");
    试一试{
    //在JSON中找到数组名称
    jsonarray=jsonobject.getJSONArray(“全部”);
    for(int i=0;i// Try this way,hope this will help you to solve your problem.
    
    @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"));
            map.put("index", i);
    
            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));
            sendtosecond.putExtra("index", arraylist.get(position).get(MainActivity.INDEX));
            Log.d("Tapped Item::", arraylist.get(position).get(MainActivity.TITLE));
            Log.d("Tapped Item Index::", arraylist.get(position).get(MainActivity.INDEX));
            startActivity(sendtosecond);
            }
            });
            }
            }
            }
    
    @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");
            strReceivedIndex = in.getStringExtra("index");
            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("all").getJSONObject(Integer.parseInt(strReceivedIndex)).getgetJSONArray("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
            }