为什么这个for循环不起作用?

时间:2013-11-27 11:57:10

标签: java android json for-loop hashmap

这是一个从两个不同的JSON URL获取数据的Android应用程序。然后我想混合他们的数据并将它们放在地图中。为此,我使用嵌套for循环。但问题是它只显示YEARS和SYSTEMDATA2而不显示SYSTEMDATA1。我认为我的嵌套循环不正确。

有谁知道原因?

for(int i = 0; i < array2System1.length(); i++){
        c1 = array2System1.getJSONObject(i);

        for(int x = 0; x < array2System2.length(); x++){

            c2 = array2System2.getJSONObject(x);

            }

        //Storing JSON item in a Variable
        valueSystem2 = c2.getString(SYSTEMDATA2);
        year = c1.getString(YEAR);
        valueSystem1 = c1.getString(SYSTEMDATA1);

         // Adding value HashMap key => value
         HashMap<String, String> map = new HashMap<String, String>();
         map.put(SYSTEMDATA1, valueSystem1);
         map.put(SYSTEMDATA2, valueSystem2);
         map.put(YEAR, year);


         mylist.add(map);
         list=(ListView)findViewById(R.id.list);

         ListAdapter adapter = new SimpleAdapter(Search.this, mylist,
                    R.layout.list_M,
                    new String[] {SYSTEMDATA1, SYSTEMDATA2, YEAR}, new int[] {
                            R.id.systemData1, R.id.systemData2, R.id.years});

         mylist.setAdapter(adapter);
    }

结果应该是这样的 年份值(SYSTEMDATA2)值(SYSTEMDATA1)

当前问题 它没有显示其中一个值。 (SYSTEMDATA1或SYSTEMDATA2) http://i40.tinypic.com/2wqykvr.png

NEW UPDATE

        //Getting JSON Array
        JSONObject myJson1 = jsons[0];
        JSONObject myJson2 = jsons[1];
        try {
            List<Map<String, String>> listValues = new ArrayList<Map<String, String>>();



            JSONArray array1C1 = myJson1.getJSONArray("myDATA");
            JSONArray array2C1 = array1C1.getJSONArray(1);

            JSONArray array1C2 = myJson2.getJSONArray("myDATA");
            JSONArray array2C2 = array1C2.getJSONArray(1);

            for (int i=0; i<array2C1.length(); i++)
            {
                JSONObject entryJsonC1 = array2C1.getJSONObject(i);


                String val1 = entryJsonC1.getString(SYSTEMDATA1);

                String year = entryJsonC1.getString("date");


                    JSONObject entryJsonC2 = array2C2.getJSONObject(i);

                    String val2 = entryJsonC2.getString(SYSTEMDATA2);


                Map<String, String> map = new HashMap<String, String>();
                map.put(SYSTEMDATA1, val1);
                map.put(SYSTEMDATA2, val2);
                map.put(YEAR, year);


                listValues.add(map);

            }

            list = (ListView) findViewById(R.id.list);


            String[] adaptersKeys = new String[] {SYSTEMDATA1, SYSTEMDATA2, YEAR};
            int[] adapterViews = new int[] {R.id.systemData1, R.id.systemData2, R.id.years};
            ListAdapter adapter = new SimpleAdapter(MultiMainActivity.this, listValues, R.layout.list2, adaptersKeys, adapterViews);

            list.setAdapter(adapter);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

现在结果如下:YEAR SAME-VALUE SAME-VALUE

5 个答案:

答案 0 :(得分:1)

for(int i=0; i<array2System1.length(); i++)
{
    c1 = array2System1.getJSONObject(i);
    year = c1.getString(YEAR);
    valueSystem1 = c1.getString(SYSTEMDATA1);

    for(int x=0; x<array2System2.length(); x++)
    {
        c2 = array2System2.getJSONObject(x);

        //Storing JSON item in a Variable
        valueSystem2 = c2.getString(SYSTEMDATA2);

        // Adding value HashMap key => value
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(SYSTEMDATA1, valueSystem1);
        map.put(SYSTEMDATA2, valueSystem2);
        map.put(YEAR, year);

        mylist.add(map);
    }
}

list = (ListView)findViewById(R.id.list);

ListAdapter adapter = new SimpleAdapter(Search.this, mylist, R.layout.list_M, new String[] {SYSTEMDATA1, SYSTEMDATA2, YEAR}, new int[] {R.id.systemData1, R.id.systemData2, R.id.years});

// Shoudl be list and not mylist
list.setAdapter(adapter);

答案 1 :(得分:1)

您需要将所有代码放在内部循环中。目前你只是关闭内部循环 - 它运行并覆盖c2并且什么都不做。

类似的东西:

for(int i = 0; i < array2System1.length(); i++){
    c1 = array2System1.getJSONObject(i);

    for(int x = 0; x < array2System2.length(); x++){
        c2 = array2System2.getJSONObject(x);

        //Storing JSON item in a Variable
        valueSystem2 = c2.getString(SYSTEMDATA2);
        year = c1.getString(YEAR);
        valueSystem1 = c1.getString(SYSTEMDATA1);

        // Adding value HashMap key => value
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(SYSTEMDATA1, valueSystem1);
        map.put(SYSTEMDATA2, valueSystem2);
        map.put(YEAR, year);

        mylist.add(map);
    }
}

答案 2 :(得分:1)

第二个for循环很早就关闭了。它应该如下。

for(int x = 0; x < array2System2.length(); x++){

            c2 = array2System2.getJSONObject(x);

        //Storing JSON item in a Variable
        valueSystem2 = c2.getString(SYSTEMDATA2);
        year = c1.getString(YEAR);
        valueSystem1 = c1.getString(SYSTEMDATA1);

         // Adding value HashMap key => value
         HashMap<String, String> map = new HashMap<String, String>();
         map.put(SYSTEMDATA1, valueSystem1);
         map.put(SYSTEMDATA2, valueSystem2);
         map.put(YEAR, year);
         mylist.add(map);
} // This is where it should get closed

否则,您只是重新分配c2变量并且丢失其他值以执行最后一个值。现在,您可以将valueSystem1,valueSystem2和YEAR值放在map中,然后添加到列表中。希望这有帮助

答案 3 :(得分:0)

List<Map<String, String>> listValues = new ArrayList<Map<String, String>>();

JSONArray jsonArray = new JSONArray(.....); // Contains all the indicators
for (int i=0; i<jsonArray.length(); i++)
{
    JSONObject entryJson = jsonArray.getJsonObject(i);

    // Check integrity
    if (!entryJson.hasKey("country")) throw new Exception("No 'country' key found");
    if (!entryJson.hasKey("value")) throw new Exception("No 'value' key found");
    if (!entryJson.hasKey("date")) throw new Exception("No 'date' key found");

    // Get country
    JSONObject countryJson = entryJson.getJsonObject("country");
    if (!countryJson.hasKey("value")) throw new Exception("No 'value' key found");
    String country = countryJson.getString("value");

    // Get population
    String population = entryJson.getString("value");

    // Get year
    String year = entryJson.getString("date");

    // Create a new Map
    Map<String, String> map = new HashMap<String, String>();
    map.put(SYSTEMDATA1, country);
    map.put(SYSTEMDATA2, population);
    map.put(YEAR, year);

    // Add to list
    listValues.add(map);
}

// Get the ListView
ListView Llist = (ListView) findViewById(R.id.list);

// Create a new adapter to attach this listView
String[] adapterKeys = new String[] {SYSTEMDATA1, SYSTEMDATA2, YEAR};
int[] adapterViews = new int[] {R.id.systemData1, R.id.systemData2, R.id.years};
ListAdapter adapter = new SimpleAdapter(Search.this, listValues, R.layout.list_M, adapterKeys, adapterViews);

// Attach the adapter to the listView
Llist.setAdapter(adapter);

这是另一种应该更好的方法。我没有测试在记事本中制作的代码。如果您有任何问题,请告诉我。

答案 4 :(得分:0)

因为你已经定义了这个:

private static final String SYSTEMDATA1 = "value";
private static final String SYSTEMDATA2 = "value";

//you can't have 2 entries with the same key
private static final String SYSTEMDATA2_KEY = "value2";

问题出现在您创建地图的位置:

// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(SYSTEMDATA1, valueSystem1);
map.put(SYSTEMDATA2_KEY, valueSystem2);
map.put(YEAR, year);

A Map是:

  

将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射一个值

来自put()方法的文档:

  

将指定的值与此映射中的指定键相关联(可选操作)。 如果地图以前包含该键的映射,则旧值将替换为指定的值。

所以你使用SYSTEMDATA1作为键放置valueSystem1,这是“值”,然后使用SYSTEMDATA2作为键放置valueSystem2,这也是“值”,所以你要覆盖valueSystem1!

请参阅下面的编辑...

修改

我猜测要检索你的值,你必须使用“值”的键,这很好,但是要在以后插入地图,你必须有唯一的键。如果您仍然有SYSTEMDATA1 =“value”和SYSTEMDATA2 =“value”,请添加一个您将用于存储在地图中以及稍后在适配器中的内容:

//you can't have 2 entries with the same key
private static final String SYSTEMDATA2_KEY = "value2";

// Create a new Map
Map<String, String> map = new HashMap<String, String>();
map.put(SYSTEMDATA1, val1);
map.put(SYSTEMDATA2_KEY, val2);
map.put(YEAR, year);

然后,当您设置适配器密钥时:

// Create a new adapter to attach this listView
String[] adapterKeys = new String[] {SYSTEMDATA1, SYSTEMDATA2_KEY, YEAR};

我的原始答案更新了代码。