改造:没有字段ID

时间:2017-08-15 12:44:59

标签: android retrofit

当我尝试获得Json结果时,我拥有我想要的所有字段但没有id字段。我不明白我给你的json结果:

[{"id":8671,"dateEvenement":"2017-08-14T16:49:34.404+02:00","type":"Competition","activitePlannings":[{"id":8675,"nomActivite":"Base-ball","idActivite":8654},{"id":8674,"nomActivite":"Balle de Hockey","idActivite":8653},{"id":8676,"nomActivite":"Course d'obstacles","idActivite":8655}],"groupe":{"id":8667,"nomGroupe":"Benjamin","groupeActivites":[{"id":8673,"nomGroupe":"Benjamin","idGroupe":8667,"nomActivite":"Balle de Hockey","idActivite":8653}]},"utilisateurPlannings":[{"id":8679,"nomUtilisateur":"Colart","prenomUtilisateur":"Pierre","type":"RESPONSABLE","datePlanning":"2017-08-14T16:49:34.404+02:00","idDisponibilite":0,"typePlanning":"Competition","nomGroupe":"Benjamin","planningId":8671,"utilisateurId":8651}],"Disponibilites":[],"validate":false}]

我确实有这个课有setter和getter:

public class Planning {
    private int id;
    private String dateEvenement;
    private Groupe groupe;
    private String type;
    private List<ActivitePlanning> activitePlannings;
    private List<UtilisateurPlanning> utilisateurPlannings;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    public Planning() {
    }


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDate() {
        return dateEvenement;
    }

    public void setDate(String date) {
        Date date2 = null;
        try {
            date2 = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        this.dateEvenement  = sdf.format(date2.getTime());
    }

    public ejplanningandroid.ejplanningandroid.Models.Groupe getGroupe() {
        return groupe;
    }

    public void setGroupe(ejplanningandroid.ejplanningandroid.Models.Groupe groupe) {
        this.groupe = groupe;
    }

    public List<UtilisateurPlanning> getUtilisateurPlannings() {
        return utilisateurPlannings;
    }

    public void setUtilisateurPlannings(List<UtilisateurPlanning> utilisateurPlannings) {
        this.utilisateurPlannings = utilisateurPlannings;
    }

    public List<ActivitePlanning> getActivitePlannings() {
        return activitePlannings;
    }

    public void setActivitePlannings(List<ActivitePlanning> activitePlannings) {
        this.activitePlannings = activitePlannings;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

我用这个类设置我的规划对象:

   @Override
    public View getView(int position, View view, ViewGroup parent) {
        if(view == null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.rowplanning,parent, false);
        }
        PlanningViewHolder planningViewHolder = (PlanningViewHolder)view.getTag();
        if(planningViewHolder==null){
            planningViewHolder= new PlanningViewHolder();
            planningViewHolder.Date = (TextView)view.findViewById(R.id.Date);
            planningViewHolder.NomActivite =(TextView)view.findViewById(R.id.Activite);
            planningViewHolder.NomGroupe = (TextView)view.findViewById(R.id.NomGroupe);
            planningViewHolder.Type = (TextView)view.findViewById(R.id.Type);
            planningViewHolder.Utilisateur = (TextView)view.findViewById(R.id.Utilisateur);
            view.setTag(planningViewHolder);
        }
        Planning planning = getItem(position);
        planningViewHolder.Type.setText("Type : "+planning.getType());
        planningViewHolder.NomGroupe.setText("Groupe : "+planning.getGroupe().getNomGroupe());
        String nomActivite = setStringFromArrayActivite(planning.getActivitePlannings());
        planningViewHolder.NomActivite.setText("Activités : "+nomActivite);
        planningViewHolder.Date.setText(planning.getDate());
        String nomUtilisateur =setStringFromArrayUtilisateur(planning.getUtilisateurPlannings());
        planningViewHolder.Utilisateur.setText("Moniteurs : "+nomUtilisateur);
        return view;
    }

我的Asynctask:

public class PlanningCandidatureTask extends AsyncTask<String,Void,List<Planning>> {
    @Override
    protected List<Planning> doInBackground(String... params) {
        try {
            InterfaceService interfaceService = new RestAdapter.Builder()
                    .setEndpoint(InterfaceService.path).build()
                    .create(InterfaceService.class);
            String login = params[0];
            String pass = params[1];
            List<Planning> PlanningList = interfaceService.getPlanningByValidation(login, pass);

            return PlanningList;
        }catch (RetrofitError retrofitError){
            return null;
        }
    }
}

但是当想要查看我是否有id时,此字段的值为0。我试图将类型更改为String但它不起作用...

已编辑:

public class CandidatureFragment extends Fragment {
    private ListView mListView;
    private View view;
    private List<Planning> listPlanning = new ArrayList<Planning>();
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.candidature_fragment, container, false);
        mListView = (ListView) view.findViewById(R.id.listViewPlanning2);

        PlanningCandidatureTask planningTask =
                (PlanningCandidatureTask) new PlanningCandidatureTask()
                        .execute(((MainActivity) getActivity()).getUtilisateur().getLogin(),
                                ((MainActivity) getActivity()).getUtilisateur().getMotDepasse());
        try {
            if(planningTask.get() != null) {
                for (int i = 0; i < planningTask.get().size(); i++) {
                    Planning planning=new Planning();
                    Log.i("Test",planningTask.get().get(i).getGroupe()+"");
                    planning.setDate(planningTask.get().get(i).getDate());
                    planning.setType(planningTask.get().get(i).getType());
                    planning.setGroupe(planningTask.get().get(i).getGroupe());
                    planning.setActivitePlannings(planningTask.get().get(i).getActivitePlannings());
                    planning.setUtilisateurPlannings(planningTask.get().get(i).getUtilisateurPlannings());
                    listPlanning.add(planning);
                }
                PlanningAdapter adapter = new PlanningAdapter(view.getContext(),listPlanning);
                mListView.setAdapter(adapter);
            }else
            {
                Toast.makeText(view.getContext(), "ERREUR DE CONNECTION", Toast.LENGTH_SHORT).show();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        final PlanningAdapter adapter = new PlanningAdapter(view.getContext(),listPlanning);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(container.getContext());
                builder.setTitle("Validation");
                builder.setMessage("Voulez vous vraiment ajouter une candidature à ce planning ?")
                        .setCancelable(false).setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Planning selectedFromList = (Planning) parent.getAdapter().getItem(position);

                        /*PostTask postTask =
                                (PostTask) new PostTask()
                                        .execute(((MainActivity) getActivity()).getUtilisateur().getLogin(),
                                                ((MainActivity) getActivity()).getUtilisateur().getMotDepasse(),
                                                "none","0");*/
                        dialog.cancel();
                        Toast.makeText(view.getContext(), "item selectionnné : "+selectedFromList.getId(), Toast.LENGTH_LONG).show();
                    }
                }).setNegativeButton("Non", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.create().show();
            }

        });

        return view;
    }

获得改造::

@GET("/planning/unvalidate/")
List<Planning> getPlanningByValidation(@Query("login") String login, @Query("password") String password);

public class PlanningViewHolder {
    public TextView Date;
    public TextView NomGroupe;
    public TextView Type;
    public TextView NomActivite;
    public TextView Utilisateur;

}

0 个答案:

没有答案