将Listview的Selected Item的特定ArrayList数据从一个活动传递到另一个活动

时间:2015-07-28 10:32:57

标签: android listview android-listview

JSON字符串:

    [{
      "name": "Gun Fu: Stickman 2",
      "price": "4",
      "rating": "4.4",

    "demographic": [
        {
            "country": "India",
            "percentage": "13"
        },
        {
            "country": "China",
            "percentage": "2"
        },
        {
            "country": "USA",
            "percentage": "61"
        }
    ]
},
{
    "name": "Call of Duty: Heroes",

    "price": "1.2",
    "rating": "4.4",
           "demographic": [
        {
            "country": "India",
            "percentage": "25"
        },
        {
            "country": "China",
            "percentage": "25"
        },
        {
            "country": "USA",
            "percentage": "50"
        }
    ]
},
{
    "name": "Batman Arkham",

    "price": "0",
    "rating": "4.2",

    "demographic": [
        {
            "country": "India",
            "percentage": "33"
        },
        {
            "country": "China",
            "percentage": "30"
        },
        {
            "country": "USA",
            "percentage": "37"
        }
    ]
},
{
    "name": "Minecraft",

    "price": "4.3",
    "rating": "4.5",

    "demographic": [
        {
            "country": "India",
            "percentage": "60"
        },
        {
            "country": "China",
            "percentage": "3"
        },
        {
            "country": "USA",
            "percentage": "37"
        }
    ]
},
{
    "name": "Clash of Clans",

    "price": "0",
    "rating": "4.6",

    "demographic": [
        {
            "country": "India",
            "percentage": "20"
        },
        {
            "country": "China",
            "percentage": "25"
        },
        {
            "country": "USA",
            "percentage": "55"
        }
    ]
},
{
    "name": "Flappy Bird",

    "price": "10",
    "rating": "3.7",

    "demographic": [
        {
            "country": "India",
            "percentage": "10"
        },
        {
            "country": "China",
            "percentage": "15"
        },
        {
            "country": "USA",
            "percentage": "75"
        }
    ]
},
{
    "name": "Hitman: Sniper",

    "price": "2",
    "rating": "4.8",

    "demographic": [
        {
            "country": "India",
            "percentage": "20"
        },
        {
            "country": "China",
            "percentage": "40"
        },
        {
            "country": "USA",
            "percentage": "40"
        }
    ]
},
{

    "price": "4.3",
    "rating": "4.8",

    "demographic": [
        {
            "country": "India",
            "percentage": "15"
        },
        {
            "country": "China",
            "percentage": "70"
        },
        {
            "country": "USA",
            "percentage": "15"
        }
    ]
},
{
    "name": "Where's My Perry?",

    "price": "0",
    "rating": "4.6",

    "demographic": [
        {
            "country": "India",
            "percentage": "33"
        },
        {
            "country": "China",
            "percentage": "33"
        },
        {
            "country": "USA",
            "percentage": "34"
        }
    ]
},
{
    "name": "Cut the Rope 2",

    "price": "6",
    "rating": "4.2",

    "demographic": [
        {
            "country": "India",
            "percentage": "40"
        },
        {
            "country": "China",
            "percentage": "30"
        },
        {
            "country": "USA",
            "percentage": "30"
        }
    ]
},
{

    "price": "0",
    "rating": "4.4",

    "demographic": [
        {
            "country": "India",
            "percentage": "83"
        },
        {
            "country": "China",
            "percentage": "7"
        },
        {
            "country": "USA",
            "percentage": "10"
        }
    ]
},
{
    "name": "TRANSFORMERS",

    "price": "0",
    "rating": "4.0",

    "demographic": [
        {
            "country": "India",
            "percentage": "1"
        },
        {
            "country": "China",
            "percentage": "0"
        },
        {
            "country": "USA",
            "percentage": "99"
        }
    ]
}
]

现在我需要的是:

  1. 将游戏名称列表添加到列表视图中。(我已经完成)
  2. 点击游戏列表视图中的任何项目后,其相关国家/地区和人口统计标记百分比应显示在新活动中。(尚待做)
  3. 请建议一些有用且耗费较少的内存消耗提示。

3 个答案:

答案 0 :(得分:1)

以下是我正在使用的代码:

在SourceActivity中: 游戏是ModelClass对象。

    intent.putExtra("country",game.getCountry());
    intent.putExtra("percentage",String.valueOf(game.getPercentage()));

在ReceiverActivity中:

  game_country=getIntent().getStringExtra("country");
  game_percentage=getIntent().getStringExtra("percentage");
  Log.d("NewCountry:",game_country);  // show in logcat//
  Log.d("\nNewPercentage:", game_percentage);  // show in logcat//

结果:

  07-28 07:47:36.956    2453-2453/? D/NewCountry:﹕ USA
  [ 07-28 07:47:36.984  2453: 2453 D/NewPercentage: ]   37 

而不是向所有相关的更多国家显示其相关百分比。

答案 1 :(得分:0)

您可以将数据解析为新活动

 Intent intent = new Intent(ActivityName.this, YOURACTIVITY.class);
 intent.putExtra("key", name);

 startActivity(intent);

read this line了解更多代码

答案 2 :(得分:0)

这样做的最佳做法是使用parcelable对象。发送parcelable对象android可以更快地支持不同组件之间的通信。 Have a look