从JSON文件填充的列表视图仅在过滤类别后返回第一个项目

时间:2014-06-18 12:45:49

标签: android json if-statement for-loop android-adapter

我使用自定义列表视图适配器从JSON文件(因为我的应用程序将要脱机)填充列表视图。列表视图根据类别进行过滤,当我运行我的应用程序时,日志猫没有给我任何错误,但列表视图只填充每个类别的项目。这是我的代码和JSON文件的示例。代码有点乱,它是一个巨大的循环。

    Intent intent = CategoryItemsActivity.this.getIntent();
    String category = (String) intent.getExtras().get("categoryName");
    int destinationId = intent.getExtras().getInt("destinationID");
    String feed = loadJSONFromAsset();
    try {
        JSONObject mJsonObj = new JSONObject(feed);
        JSONArray jsonArray = mJsonObj.getJSONArray("partners");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String name = jsonObject.getString("name");
            String categoryname = jsonObject.getString("category_name");
            Double lat = jsonObject.getDouble("lat");
            Double lng = jsonObject.getDouble("lang");
            String address = jsonObject.getString("address");
            int destid = jsonObject.getInt("destination_id");
            String thumbnail_image = jsonObject
                    .getString("thumbnail_image");
            compare.put(name, destid);
            hashmapNames.put(categoryname, name);
            hashmapAddress.put(name, address);
            thumbnailimages.put(name, thumbnail_image);
            latitudes.put(name, lat);
            longitudes.put(name, lng);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    for (Entry<String, Integer> entry1 : compare.entrySet()) {
        String key1 = entry1.getKey();
        int value1 = entry1.getValue();
        if (destinationId == value1) {
            for (Entry<String, String> entry2 : hashmapNames.entrySet()) {
                String key2 = entry2.getKey();
                String value2 = entry2.getValue();
                for (Entry<String, String> entry3 : hashmapAddress
                        .entrySet()) {
                    String key3 = entry3.getKey();
                    String value3 = entry3.getValue();
                    for (Entry<String, String> entry4 : thumbnailimages
                            .entrySet()) {
                        String key4 = entry4.getKey();
                        String value4 = entry4.getValue();
                        if (category.equals(key2) && key1.equals(value2)
                                && key3.equals(value2)
                                && key4.equals(value2)) {

                            setTitle(key2);
                            addresses.add(value3);
                            names.add(key1);
                            int resID = getResources().getIdentifier(
                                    value4,
                                    "drawable",
                                    CategoryItemsActivity.this
                                            .getPackageName());
                            images.add(resID);
                            rowItems = new ArrayList<RowItem>();
                            for (int x = 0; x < names.size(); x++) {
                                RowItem item = new RowItem(
                                        images.get(x), names.get(x),
                                        addresses.get(x));
                                rowItems.add(item);
                            }
                            CustomListViewAdapter adapter1 = new CustomListViewAdapter(
                                    this, R.layout.list_item_2, rowItems);
                            listview.setAdapter(adapter1);
                            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                @Override
                                public void onItemClick(
                                        AdapterView<?> parent, View view,
                                        int position, long id) {
                                    RowItem name = (RowItem) parent
                                            .getItemAtPosition(position);
                                    Intent i = new Intent(
                                            CategoryItemsActivity.this,
                                            PartnersInfo.class);
                                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    i.putExtra("PartnerName",
                                            name.toString());
                                    startActivity(i);

                                }
                            });
                            for (Entry<String, Double> entry5 : longitudes
                                    .entrySet()) {
                                String key5 = entry5.getKey();
                                Double value5 = entry5.getValue();
                                for (Entry<String, Double> entry6 : latitudes
                                        .entrySet()) {
                                    String key6 = entry6.getKey();
                                    Double value6 = entry6.getValue();
                                    if (key5.equals(value2)
                                            && key6.equals(value2)) {

                                        Marker marker = map
                                                .addMarker(new MarkerOptions()
                                                        .position(
                                                                new LatLng(
                                                                        value6,
                                                                        value5))
                                                        .title(key5));
                                    }
                                }
                            }

                        }
                    }
                }

            }
        }

    }

}

这是我的JSON文件:

    {
"partners": [
    {
        "id": "1",
        "name": "Bunk Hostel",
        "location_name": "Beyoglu",
        "destination_id":"004",
        "category_name": "Accommodation",
        "thumbnail_image":"thumbnail",
        "address": "Bunk Hostel Address",
        "stringInfo": "this text is for bunk hostel",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    }, 
    {
        "id": "2",
        "name": "Accomo",
        "location_name": "Beyoglu",
        "destination_id":"004",
        "category_name": "Accommodation",
        "thumbnail_image":"thumbnail",
        "address": "sample Address",
        "stringInfo": "this text is for sample",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    }, 
    {
        "id": "3",
        "name": "Tamazra Tour",
        "location_name": "Beyoglu",
        "destination_id":"004",
        "category_name": "Tours",
        "address": "Tamazra Address",
        "stringInfo": "this text is for tamazra tour",
        "thumbnail_image": "thumbnail",
        "lat":"41.035527",
        "lang":"28.978282",
        "url": "hikeastUrl"
    },
    {
        "id": "4",
        "name": "new Tour",
        "location_name": "Beyoglu",
        "destination_id":"004",
        "category_name": "Tours",
        "address": "new Address",
        "stringInfo": "this text is for new tour",
        "thumbnail_image": "thumbnail",
        "lat":"41.035527",
        "lang":"28.978282",
        "url": "hikeastUrl"
    },
    {
        "id": "5",
        "name": "Arsen Lupen",
        "location_name": "Beyoglu",

        "destination_id":"004",
        "category_name": "Pubs",
        "address": "Arsen Lupen Address",
        "stringInfo": "this text is for Arsen Lupen",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    }, {
        "id": "6",
        "name": "Arsen new",
        "location_name": "Beyoglu",

        "destination_id":"004",
        "category_name": "Pubs",
        "address": "Arsen new Address",
        "stringInfo": "this text is for Arsen Lupen",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    },
    {
        "id": "7",
        "name": "Cukur Meyhane",
        "location_name": "Beyoglu",

        "destination_id":"004",
        "category_name": "Restaurants",
        "address": "Cukur Meyhane Address",
        "stringInfo": "this text is for Cukur Meyhane",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    },
    {
        "id": "8",
        "name": "new restaurant",
        "location_name": "Beyoglu",

        "destination_id":"004",
        "category_name": "Restaurants",
        "address": "new restaurant Address",
        "stringInfo": "this text is for Cukur Meyhane",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    },
    {
        "id": "9",
        "name": "Aponia Stores",
        "location_name": "Beyoglu",
        "destination_id": "004",
        "category_name": "Shops",
        "address": "Aponia Adress Address",
        "stringInfo": "this text is for Aponia",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    },
    {
        "id": "10",
        "name": "Artwalk",
        "location_name": "Beyoglu",
        "destination_id":"004",
        "category_name": "Activities",
        "address": "artwalk Address",
        "stringInfo": "this text is for artwalk",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    },
    {
        "id": "11",
        "name": "new actv",
        "location_name": "Beyoglu",
        "destination_id":"004",
        "category_name": "Activities",
        "address": "new actv Address",
        "stringInfo": "this text is for artwalk",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    },
    {
        "id": "12",
        "name": "Sample",
        "location_name": "",
        "destination_id": "004",
        "category_name": "Shops",
        "address": "Sample",
        "stringInfo": "this text is for Sample",
        "thumbnail_image": "thumbnail",
        "lat":"41.034898",
        "lang":"28.982929",
        "url": "hikeastUrl"
    }


]
}

0 个答案:

没有答案