ArrayList重复条目?

时间:2014-12-11 15:17:38

标签: android json arraylist

在我的应用程序中,我有一个JSON,它包含一个JSONArray到JSONObject中。我使用JSONArray选择了这些JSONObject并生成了一个对象Batalha并且还拾取了JSONArray(“徽章”)并添加到对象BadgesGanha中。在对象Batalha我有一个List,我添加了对象BadgeGanha。

现在我正在尝试在对象Batalha中打印List的大小,问题是打印一个重复的值,例如,如果List在logcat打印6中有大小3.我无法理解为什么会出现这个问题。

JSON

"retorno": {
        "batalhas": {
            "aberto": [
                {
                    "ID": "17",
                    "player1": "fernando@domain.com",
                    "player2": "joao@msn.com",
                    "data_inicio": "2014-12-05 16:32:03",
                    "data_fim": null,
                    "status": "1",
                    "ID_arena": "1",
                    "vez_player": "joao@msn.com",
                    "player_vencedor": null,
                    "adversario": [
                        {
                            "ID": "9",
                            "nome": "Joao Paulo",
                            "email": "joao@msn.com",
                            "foto": "e8357f1a1d05334f08c84d2de0049234.png",
                            "status": "1",
                            "online": "0",
                            "pontuacao": "1260",
                            "graduacao": "Ministro"
                        }
                    ],

                    "badges": [
                        {
                            "ID": "41",
                            "email": "joao@msn.com",
                            "ID_batalha": "17",
                            "ID_arena": "1",
                            "status": "0"
                        },
                        {
                            "ID": "45",
                            "email": "fernando@domain.com",
                            "ID_batalha": "17",
                            "ID_arena": "1",
                            "status": "0"
                        },
                        {
                            "ID": "51",
                            "email": "fernando@domain.com",
                            "ID_batalha": "17",
                            "ID_arena": "1",
                            "status": "0"
                        },

                    ]
                },

public class Batalha implements Serializable{
    private static final long serialVersionUID = 1L;

    @SerializedName("ID")
    private Long id;

    private String player1;
    private String player2;
    private String data_inicio;
    private String data_fim;
    private int status;
    private int ID_arena;
    private String vez_player;
    private String player_vencedor;

    private List<BadgesGanha> badges = new ArrayList<BadgesGanha>();


    public Batalha() {    
    }


    /** add badges ganha */
    public void addBadgesGanha(BadgesGanha badge){
        badges.add(badge);
    }



public class BadgesGanha implements Serializable{
    private static final long serialVersionUID = 1L;

    @SerializedName("ID")
    private Integer id;

    private String email;
    private Integer ID_batalha;
    private Integer ID_arena;
    private Integer status;

创建列表并显示列表大小

Gson gson = new Gson();
JSONObject objRetorno = obj.getJSONObject("retorno");

/** abertas */
JSONObject objBatalhas = objRetorno.getJSONObject("batalhas");
JSONArray arrayAberto = objBatalhas.getJSONArray("aberto");
List<Batalha> listBatalhaAberta = new ArrayList<Batalha>();

//batalha aberta
for(int x = 0; x < arrayAberto.length(); x++){
    //pega batalha
    JSONObject jsObj = arrayAberto.getJSONObject(x);
    Batalha batalha = gson.fromJson(jsObj.toString(), Batalha.class);

    //pega badges
    JSONArray arrayBadges = jsObj.getJSONArray("badges");
    for(int i = 0; i < arrayBadges.length(); i++){
        JSONObject jsBadge = arrayBadges.getJSONObject(i);
        BadgesGanha badge = gson.fromJson(jsBadge.toString(), BadgesGanha.class);
        //add badge batalha
        batalha.addBadgesGanha(badge);
    }
    //create list of Batalha
    listBatalhaAberta.add(batalha);
}

//print size list List<BadgesGanha>
for(Batalha x : listBatalhaAberta){
   //should print 3 badges but print 6 badges
   Log.i("BADGES LISTBATALHAABERTA-> ", x.getBadges().size() + "");
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

我更改了块徽章,现在可以使用了!

//badges
JSONArray arrayBadges = jsObj.getJSONArray("badges");
List<BadgesGanha> lista = new ArrayList<BadgesGanha>();
for(int i = 0; i < arrayBadges.length(); i++){
    JSONObject jsBadges = arrayBadges.getJSONObject(i);
    BadgesGanha badge = gson.fromJson(jsBadges.toString(), BadgesGanha.class);
    lista.add(badge);
    batalha.setBadges(lista);
}