Blame view

app/src/main/java/net/plil/clubinfo/etunicorn/app/consomation/CreateConsomation.java 3.62 KB
73b70b1e   badetitou   Ajout de Consomation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  package net.plil.clubinfo.etunicorn.app.consomation;
  
  import android.app.Dialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.v4.app.DialogFragment;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.AlertDialog;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  import android.widget.ProgressBar;
  
  import com.android.volley.Request;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.toolbox.JsonObjectRequest;
  
  import net.plil.clubinfo.etunicorn.R;
  import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  
  public class CreateConsomation extends DialogFragment {
  
      EditText mNomConsomation;
      EditText mPrice;
      ProgressBar mProgressBar;
  
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          // Use the Builder class for convenient dialog construction
          LayoutInflater inflater = getActivity().getLayoutInflater();
          View view = inflater.inflate(R.layout.fragment_create_consomation, null);
          mNomConsomation = (EditText) view.findViewById(R.id.create_consomation_name);
          mPrice = (EditText) view.findViewById(R.id.create_consomation_price);
          mProgressBar = (ProgressBar) view.findViewById(R.id.create_consomation_progress_bar);
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder
                  .setTitle(R.string.create_consomation)
                  .setView(view)
                  .setPositiveButton(R.string.valid, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
  
                          mProgressBar.setVisibility(View.VISIBLE);
                          mPrice.setVisibility(View.GONE);
                          mNomConsomation.setVisibility(View.GONE);
                          JSONObject jsonObject = new JSONObject();
                          try {
                              jsonObject.put("nomConsomation", mNomConsomation.getText().toString());
                              jsonObject.put("prix", Double.parseDouble(mPrice.getText().toString()));
                          } catch (JSONException e){
                              e.printStackTrace();
                          }
                          JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/transaction/debit"   ,jsonObject , new Response.Listener<JSONObject>() {
                              @Override
                              public void onResponse(JSONObject response) {
                                  mProgressBar.setVisibility(View.GONE);
                              }
                          }, new Response.ErrorListener() {
                              @Override
                              public void onErrorResponse(VolleyError error) {
                                  mProgressBar.setVisibility(View.GONE);
                              }
                          }
                          );
  
                          VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
                      }
                  })
                  .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          // User cancelled the dialog
                      }
                  });
          // Create the AlertDialog object and return it
          return builder.create();
      }
  
  
  }