samedi 1 avril 2017

how to post using checkbox inside fragments... to server through volley registration process?

i have created a registration function using volley library... here i am posting some data to mydatabase tables to post to some table i want to use checkbox and these checkboxes i have inside my different fragments. all i want to do is when a checkbox is checked i want it to pass some value and if not checked i want it to pass "N.A" text Through "private void registerUser" function, which runs on click of a register button. what is the method of doing it... i want the code.

Here's my Main activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_Name, et_Pin;
    private Button RegisterBTN;
    private ProgressDialog progressDialog;

    private Spinner sDropdown;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_Name = (EditText)findViewById(R.id.etName);
        et_Pin = (EditText)findViewById(R.id.etPin);
        RegisterBTN = (Button)findViewById(R.id.btn_Reg);

        progressDialog = new ProgressDialog(this);

        adapter = ArrayAdapter.createFromResource(this, R.array.spinner_options, android.R.layout.simple_spinner_item);

        spinnerListner();

        RegisterBTN.setOnClickListener(this);


    }

    public void spinnerListner(){
        sDropdown = (Spinner)findViewById(R.id.spinner2);
        sDropdown.setAdapter(adapter);
        sDropdown.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        switch (position){
                            case 0:
                               getSupportFragmentManager().beginTransaction().replace(R.id.frag, Info_frag.newInstance()).addToBackStack(null).commit();
                                break;
                            case 1:
                               getSupportFragmentManager().beginTransaction().replace(R.id.frag, Plumber_frag.newInstance()).addToBackStack(null).commit();
                                break;
                            case 2:
                                getSupportFragmentManager().beginTransaction().replace(R.id.frag, Painter_frag.newInstance()).addToBackStack(null).commit();
                                break;
                            case 3:
                                getSupportFragmentManager().beginTransaction().replace(R.id.frag, Electrician_frag.newInstance()).addToBackStack(null).commit();
                               
                                break;
                        }

                        /**
                         * TextView spinnerDailogText = (TextView)view;
                        Toast.makeText(MainActivity.this, "You selected:"+ spinnerDailogText.getText(), Toast.LENGTH_SHORT).show();*/
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                }
        );
    }


    private void registerUser(){
        final String name = et_Name.getText().toString().trim();
        final String pin = et_Pin.getText().toString().trim();
        final String a = (/**Here i want to add checkbox a to retrive value*/);
        final String b = (/**Here i want to add checkbox b to retrive value*/);
        final String c = (/**Here i want to add checkbox c to retrive value*/);
        final String x = (/**Here i want to add checkbox x to retrive value*/);
        final String y = (/**Here i want to add checkbox y to retrive value*/);
        final String z = (/**Here i want to add checkbox z to retrive value*/);

        progressDialog.setMessage("Registering please wait...");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                Constants.REGISTER,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        progressDialog.dismiss();

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        progressDialog.hide();
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();

                /** Here I'll post the values to server*/

                params.put("reg_name", name);
                params.put("reg_pin", pin);
                params.put("reg_pin", a);
                params.put("reg_pin", b);
                params.put("reg_pin", c);
                params.put("reg_pin", x);
                params.put("reg_pin", y);
                params.put("reg_pin", z);

                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    @Override
    public void onClick(View v) {

        if (v == RegisterBTN);
            registerUser();
            
    }

It's my Fragment.java

public class Electrician_frag extends Fragment {

    private CheckBox checkZ, checkC;

    public static Electrician_frag newInstance() {
        Electrician_frag fragment = new Electrician_frag();
        return fragment;
    }

    public Electrician_frag(){

    }

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

          checkZ = (CheckBox)v.findViewById(R.id.checkBox_z);
          checkC = (CheckBox)v.findViewById(R.id.checkBox_c);

          return v;
          }
}

this is the Fragment.Xml

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <CheckBox
            android:id="@+id/checkBox_z"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="124dp"
            android:layout_marginStart="124dp"
            android:text="@string/ho"
            android:textSize="20sp"
            android:textStyle="bold" />

        <CheckBox
            android:id="@+id/checkBox_c"
            style="@android:style/Widget.Holo.Light.CompoundButton.CheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="70dp"
            android:layout_marginStart="70dp"
            android:layout_toEndOf="@+id/checkBox_z"
            android:layout_toRightOf="@+id/checkBox_z"
            android:text="@string/d"
            android:textSize="20sp"
            android:textStyle="bold" />

    </RelativeLayout>
</FrameLayout>



Aucun commentaire:

Enregistrer un commentaire