使用对象过滤List?

时间:2014-12-11 12:29:40

标签: android json

在我的应用程序中,我有一个JSONArray,其中包含一些徽章信息。我得到这些信息并添加一个Object(Batalha)并生成一个包含所有信息的List<Batalha>。现在,我需要过滤此列表来计算玩家拥有的徽章数量。徽章参考是ID_Arena。问题是我不知道为什么复制ListAdapter中的信息。

我该怎么做?

这里。

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"
                        },
                        {
                            "ID": "52",
                            "email": "fernando@domain.com",
                            "ID_batalha": "17",
                            "ID_arena": "1",
                            "status": "0"
                        },
                        {
                            "ID": "53",
                            "email": "fernando@domain.com",
                            "ID_batalha": "17",
                            "ID_arena": "4",
                            "status": "0"
                        },
                        {
                            "ID": "54",
                            "email": "fernando@domain.com",
                            "ID_batalha": "17",
                            "ID_arena": "4",
                            "status": "0"
                        }
                    ]
                },

获取并准备JSON

/** get a List<Batalha> that contain batalha informations/opponent/badges */
    public ApplicationController getAllBattles(final BatalhaAdapter listener){
    ApplicationController apc = new ApplicationController(urlGet.toString(), 
                                                                  null, 
                                                                  new Response.Listener<JSONObject>() {
                                                                        @Override
                                                                        public void onResponse(JSONObject obj) {
                                                                            //define batalha
                                                                            try {
                                                                                JSONObject objRetorno = obj.getJSONObject("retorno");

                                                                                /** abertas */
                                                                                JSONObject objBatalhas = objRetorno.getJSONObject("batalhas");                                                                                                                                                      
                                                                                Gson gson = new Gson();

                                                                                JSONArray arrayAberto = objBatalhas.getJSONArray("aberto");
                                                                                List<Batalha> listBatalhaAberta = new ArrayList<Batalha>();

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

                                                                                    //adversario
                                                                                    JSONArray arrayAdversario = jsObj.getJSONArray("adversario");
                                                                                    for(int i = 0; i < arrayAdversario.length(); i++){
                                                                                        JSONObject jsAdvers = arrayAdversario.getJSONObject(i);
                                                                                        Usuario usuario = gson.fromJson(jsAdvers.toString(), Usuario.class);
                                                                                        batalha.addAdversario(usuario);
                                                                                    }

                                                                                    //badges
                                                                                    JSONArray arrayBadges = jsObj.getJSONArray("badges");
                                                                                    for(int i = 0; i < arrayBadges.length(); i++){
                                                                                        Log.i("BATALHA_ID_GETALLBATTLES->", batalha.getId() + "");
                                                                                        JSONObject jsBadge = arrayBadges.getJSONObject(i);
                                                                                        BadgesGanha badge = gson.fromJson(jsBadge.toString(), BadgesGanha.class); 
                                                                                        batalha.addBadgesGanha(badge);                                                                                  
                                                                                    }
                                                                                    ////
                                                                                    listBatalhaAberta.add(batalha);
                                                                                }
    listener.getAllBattlesOpened(listBatalhaAberta);

定义ListAdapter

 private void getAllBattles(){
            ApplicationController app = new BatalhaDAO().getAllBattles(new BatalhaAdapter(){
                @Override
                public void getAllBattlesOpened(List<Batalha> list) {
                    if(!list.isEmpty()){
                        listBatalhaAberto = list;
                        if(jogosListAdapterAndamento == null){
                            jogosListAdapterAndamento = new JogosListAdapter(getView().getContext(), listBatalhaAberto);
                            lvJogosAndamento.setAdapter(jogosListAdapterAndamento);
                        }else{
                            jogosListAdapterAndamento.changeList(listBatalhaAberto);
                        }
                    }
                }

ListAdapter

public class JogosListAdapter extends BaseAdapter {
    private List<Batalha> lista;
    private Context context;    

    public JogosListAdapter(Context context, List<Batalha> list){
        this.context = context;
        this.lista = list;
    }

    @Override
    public int getCount() {
        return lista.size();
    }

    @Override
    public Object getItem(int position) {
        return lista.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void changeList(List<Batalha> lista){
        this.lista = lista;
        notifyDataSetChanged();     
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        Batalha batalha = lista.get(position);
        View layout;
        if (view == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.jogos_list_adapter, parent, false);
        }else{
            layout = view;
        }

      //get and count badges of usuario
      //badges
        int bgP1 = 0;
        int bgP2 = 0;
        if(!batalha.getBadges().isEmpty()){
            for(BadgesGanha bg : batalha.getBadges()){
                if(Long.parseLong(String.valueOf(bg.getID_batalha())) == batalha.getId()){
                    if(bg.getEmail().equals("fernando@domain.com")){
                        bgP1++;
                    }else{
                        bgP2++;
                    }
                }
            }
        }

    tvValorJogador.setText(String.valueOf(bgP1));
            tvRivalJogador.setText(String.valueOf(bgP2));

Batalha Bean

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<Usuario> adversarios = new ArrayList<Usuario>();
    private List<BadgesGanha> badges = new ArrayList<BadgesGanha>();

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

问题在于我的方法getAllBattles复制了我的List。我改变它现在有效。