GET方法返回错误值

时间:2018-11-08 11:24:14

标签: arraylist model getter-setter getter empty-list

我试图填充observersIDs ArrayList,然后将其作为参数放入我的TicketModel对象的设置器ticket.setObserverIDs(observersIDs)中。

在调用设置器之前,我正在按照DisplayArrayList(observersIDs)的顺序显示ArrayList内容。幸运的是,该列表包含了我想要的值。

但是,当我在填充ListView(在循环外部)并显示其内容后调用吸气剂时,ArrayList为空。我很确定这是因为我要在每个TicketModels.add(ticket);之后清除我的ArrayList(我确实需要该指令),但这并没有意义,因为我没有显示该ArrayList,而是打电话给我已填充的TicketModek对象的getter。

这是我的课程:

public class ListTickets extends Fragment {

    private ArrayList<TicketModel> TicketModels;
    private ArrayList<String> observersIDs;
    private ListView listView;
    private static TicketAdapter adapter;

    private RequestQueue queue;
    private String motifAttente;
    private String titreTicket, slaTicket, urgenceTicket, idTicket, demandeurTicket,
            categorieTicket, etatTicket, dateDebutTicket, statutTicket,
            dateEchanceTicket, dateClotureTicket, dateResolutionTicket, descriptionTicket, lieuTicket;

    public static Handler handlerticket;
    boolean ticketEnretard;


    public ListTickets() {
        handlerticket = new HandlerTicket();
        Log.d("INITIALIZATION","J'ai intialisé le handler en cours !");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_tickets, container, false);

        initView(view);    
        registerForContextMenu(listView);
        getTicketsHTTP();

        return view;
    }



    private void initView(View view) {
        TicketModels = new ArrayList<>();
        observersIDs = new ArrayList<>();
        queue = Volley.newRequestQueue(getActivity());
        listView = view.findViewById(R.id.list);
    }

    private void getTicketsHTTP() {
        String url = Constants.GLPI_URL+"search/Ticket";

        int maxRange = range-1;
        List<KeyValuePair> params = new ArrayList<>();
        //TECHNICIEN = IDUSER
        params.add(new KeyValuePair("criteria[0][field]","5"));
        params.add(new KeyValuePair("criteria[0][searchtype]","equals"));
        params.add(new KeyValuePair("criteria[0][value]",idUser));
        //AND STATUT EST EN COURS (ATTIRIBUE)
        params.add(new KeyValuePair("criteria[1][link]","AND"));
        params.add(new KeyValuePair("criteria[1][field]","12"));
        params.add(new KeyValuePair("criteria[1][searchtype]","equals"));
        params.add(new KeyValuePair("criteria[1][value]","2"));
        //OU TECHNICIEN = IDUSER
        params.add(new KeyValuePair("criteria[2][link]","OR"));
        params.add(new KeyValuePair("criteria[2][field]","5"));
        params.add(new KeyValuePair("criteria[2][searchtype]","equals"));
        params.add(new KeyValuePair("criteria[2][value]",idUser));
        //AND STATUT EST EN COURS (PLANIFIE)
        params.add(new KeyValuePair("criteria[3][link]","AND"));
        params.add(new KeyValuePair("criteria[3][field]","12"));
        params.add(new KeyValuePair("criteria[3][searchtype]","equals"));
        params.add(new KeyValuePair("criteria[3][value]","3"));      
       //AFFICHAGE
        params.add(new KeyValuePair("forcedisplay[0]","4"));
        params.add(new KeyValuePair("forcedisplay[1]","10"));
        params.add(new KeyValuePair("forcedisplay[2]","7"));
        params.add(new KeyValuePair("forcedisplay[3]","12"));
        params.add(new KeyValuePair("forcedisplay[4]","15"));
        params.add(new KeyValuePair("forcedisplay[5]","30"));
        params.add(new KeyValuePair("forcedisplay[6]","18"));
        params.add(new KeyValuePair("forcedisplay[7]","21"));
        params.add(new KeyValuePair("forcedisplay[8]","83"));
        params.add(new KeyValuePair("forcedisplay[9]","82"));
        params.add(new KeyValuePair("forcedisplay[10]","16"));
        params.add(new KeyValuePair("forcedisplay[11]","2"));
        params.add(new KeyValuePair("forcedisplay[12]","17"));
        params.add(new KeyValuePair("forcedisplay[13]","66"));
        //ORDRE ET RANGE
        params.add(new KeyValuePair("sort","15"));
        params.add(new KeyValuePair("order","DESC"));
        params.add(new KeyValuePair("range","0-"+maxRange+""));

        final JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, generateUrl(url, params), null,
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                            JSONArray Jdata = response.getJSONArray("data");
                            for (int i=0; i < Jdata.length(); i++) {
                                try {
                                    JSONObject oneTicket = Jdata.getJSONObject(i);

                                    titreTicket = oneTicket.getString("1");
                                    slaTicket = oneTicket.getString("30");
                                    dateDebutTicket = oneTicket.getString("15");
                                    urgenceTicket = oneTicket.getString("10");
                                    statutTicket = oneTicket.getString("12");
                                    idTicket = oneTicket.getString("2");

                                    demandeurTicket = oneTicket.getString("4");
                                    categorieTicket = oneTicket.getString("7");
                                    etatTicket = oneTicket.getString("12");
                                    dateEchanceTicket = oneTicket.getString("18");
                                    descriptionTicket = oneTicket.getString("21");

                                    lieuTicket = oneTicket.getString("83");
                                    dateClotureTicket = oneTicket.getString("16");
                                    dateResolutionTicket = oneTicket.getString("17");
                                    ticketEnretard = getBooleanFromSt(oneTicket.getString("82"));

                                    observateur = oneTicket.getString("66");

                                    observersIDs.clear();
                                    try {
                                        JSONArray JObs = oneTicket.getJSONArray("66");
                                        Log.d("JSONARRAY OBS N°"+i+" ("+titreTicket+")","Observer converted to JSONArray !");
                                        for (int j=0; j < JObs.length(); j++) {
                                            try {
                                                String oneObservateur = JObs.getString(j);
                                                observersIDs.add(oneObservateur);
                                            } catch (JSONException e) {
                                                Log.e("Error Observateur ", e.getMessage());
                                            }
                                        }
                                    } catch (JSONException e) {
                                        Log.e("JSONARRAY OBSERVER ("+titreTicket+")","Observer cannot be converted to JSONArray...");
                                        observersIDs.add(observateur);
                                    }

                                } catch (JSONException e) {
                                    Log.e("Nb of data: "+Jdata.length()+" || "+"Error JSONArray at "+i+" : ", e.getMessage());
                                }

                                /* ---------  Creating a TicketModel object  --------- */

                                TicketModel ticket = new TicketModel(titreTicket, slaTicket, dateDebutTicket,
                                        calculTempsRestant(dateEchanceTicket), idTicket, statutTicket);

                                ticket.setUrgenceTicket(urgenceText(urgenceTicket));
                                ticket.setTicketEnRetard(Boolean.parseBoolean(String.valueOf(ticketEnretard)));
                                ticket.setDescription(descriptionTicket);
                                ticket.setDemandeurID(demandeurTicket);

                                System.out.println("____ Testing Obs ArrayList during Model creation ____");

                                DisplayArrayList(observersIDs); //THE CORRECT ARRAYLIST IS DISPLAYED

                                ticket.setObserverIDs(observersIDs); //I SET IT JUST AFTER SINCE IT'S ALWAYS THE CORRECT VALUE

                                TicketModels.add(ticket);

                                observersIDs.clear();

                                /* ---------  Creating a TicketModel object  --------- */

                            }


                            if (getActivity() != null){
                                adapter = new TicketAdapter(TicketModels,getActivity());
                            }
                            else{
                                Log.e("ERROR", "An Error was about to occur");
                            }


                            listView.setAdapter(adapter);


                            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                                    TicketModel TicketModel = TicketModels.get(position);


                                    DisplayArrayList(TicketModel.getObserverIDs()); //I GET A EMPTY ARRAYLIST


                                    startActivity(i);

                                }
                            });


                        } catch (JSONException e) {
                            Log.e("malkach",e.getMessage());
                        }


                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Error.Response!", error.toString());
                    }

                }
        ){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("App-Token",Constants.App_Token);
                params.put("Session-Token",session_token);
                return params;
            }

        };

        // add it to the RequestQueue
        queue.add(getRequest);

    }


    private void DisplayArrayList(ArrayList list) {
        System.out.println("\n --- ArrayList --- \n");
        for (int i = 0; i < list.size(); i++){
            String oneObs = (String)list.get(i);
            System.out.println(oneObs);
        }
    }

}

这是我的TicketModel类:

public class TicketModel {
    private String titreTicket;
    private String slaTicket;
    private String DateTicket;
    private String UrgenceTicket;
    private boolean ticketEnRetard;
    private String TempsRestantTicket;
    private String idTicket;
    private String Statut;
    private String TempsResolution;
    private String TempsRetard;
    private String description;
    private String demandeurID;
    private ArrayList<String> observerIDsss;


    public TicketModel(String titreTicket, String slaTicket, String dateTicket, String tempsRestantTicket, String idticket, String statut) {
        this.titreTicket = titreTicket;
        this.slaTicket = slaTicket;
        DateTicket = dateTicket;
        TempsRestantTicket = tempsRestantTicket;
        idTicket = idticket;
        Statut = statut;
    }

    public String getTitreTicket() {
        return titreTicket;
    }

    public String getSlaTicket() {
        return slaTicket;
    }

    public String getDateTicket() {
        return DateTicket;
    }

    public String getUrgenceTicket() {
        return UrgenceTicket;
    }

    public void setUrgenceTicket(String urgenceTicket) {
        UrgenceTicket = urgenceTicket;
    }

    public void setTempsRestantTicket(String tempsRestantTicket) { TempsRestantTicket = tempsRestantTicket; }

    public String getTempsRestantTicket() {
        return TempsRestantTicket;
    }

    public boolean isTicketEnRetard() {
        return ticketEnRetard;
    }

    public void setTicketEnRetard(boolean ticketEnRetard) {
        this.ticketEnRetard = ticketEnRetard;
    }

    public String getIdTicket() { return idTicket; }

    public String getStatut() { return Statut; }

    public String getTempsResolution() {
        return TempsResolution;
    }

    public void setTempsResolution(String tempsResolution) {
        TempsResolution = tempsResolution;
    }

    public String getTempsRetard() {
        return TempsRetard;
    }

    public void setTempsRetard(String tempsRetard) {
        TempsRetard = tempsRetard;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDemandeurID(String demandeurID) {
        this.demandeurID = demandeurID;
    }

    public String getDemandeurID() {
        return demandeurID;
    }


    public void setObserverIDs(ArrayList<String> observerIDs) {
        this.observerIDsss = observerIDs;
    }

    public ArrayList<String> getObserverIDs() {
        return observerIDsss;
    }



}

有人可以向我解释为什么在调用吸气剂时我没有得到正确的值吗?

0 个答案:

没有答案