Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get a spinner on Android using volley to store Gender data into a mySQL database.
 here's the source code


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.carldwalters.communications.create_account"
    tools:showIn="@layout/activity_create_account">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="First name"
            android:id="@+id/first_nameTextView"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="1900dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text=""
            android:ems="10"
            android:id="@+id/first_nameEditText"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="33dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Middle name"
            android:id="@+id/middleNameTextTiew"
            android:layout_below="@+id/first_nameEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="33dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text=""
            android:ems="10"
            android:id="@+id/middle_nameEditText"
            android:layout_below="@+id/middleNameTextTiew"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Last name"
            android:id="@+id/lastNameTextView"
            android:layout_below="@+id/middle_nameEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="36dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text=""
            android:ems="10"
            android:id="@+id/last_nameEditText"
            android:layout_below="@+id/lastNameTextView"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="E-mail"
            android:id="@+id/e_mailTextView"
            android:layout_below="@+id/last_nameEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="36dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/e_mailEditText"
            android:layout_below="@+id/e_mailTextView"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="User name"
            android:id="@+id/user_nameTextView"
            android:layout_below="@+id/e_mailEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/user_nameEditText"
            android:layout_below="@+id/user_nameTextView"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Date of birth"
            android:id="@+id/dateOfBirthTextView"
            android:layout_below="@+id/user_nameEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/date_of_birthEditText"
            android:layout_below="@+id/dateOfBirthTextView"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Gender"
            android:layout_marginTop="629dp"
            android:id="@+id/genderTextView" />



        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/genderSpinner"
            android:layout_marginTop="660dp"
            android:layout_centerHorizontal="true" />


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/genderEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="740dp" />











        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Password"
            android:id="@+id/passwordTextView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="900dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/passwordEditText"
            android:layout_below="@+id/passwordTextView"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Confirm password"
            android:id="@+id/confirmPasswordTextView"
            android:layout_below="@+id/passwordEditText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/confirm_passwordEditText"
            android:layout_below="@+id/confirmPasswordTextView"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="By clicking submit you agree to the terms and services and private policy."
            android:id="@+id/legalTextView"
            android:layout_marginTop="1600dp"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Terms and conditions"
            android:id="@+id/terms_and_conditionsButton"
            android:layout_marginTop="1701dp"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Private policy"
            android:id="@+id/private_policyButton"
            android:layout_alignParentStart="true"
            android:layout_marginTop="1750dp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:id="@+id/SubmitButton"
            android:layout_alignParentStart="true"
            android:layout_marginTop="1800dp" />




    </RelativeLayout>
    </ScrollView>
</RelativeLayout>


package com.example.carldwalters.communications;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import android.app.AlertDialog;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import static android.R.layout.simple_spinner_item;


public abstract class create_account  extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener{

   // EditText genderEditText;
    ArrayAdapter<CharSequence> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_account);
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // setSupportActionBar(toolbar);
        //  showDialogOnButtonClick();


        final EditText first_nameEditText = (EditText) findViewById(R.id.first_nameEditText);
        final EditText middle_nameEditText = (EditText) findViewById(R.id.middle_nameEditText);
        final EditText last_nameEditText = (EditText) findViewById(R.id.last_nameEditText);
        final EditText e_mailEditText = (EditText) findViewById(R.id.e_mailEditText);
        final EditText user_nameEditText = (EditText) findViewById(R.id.user_nameEditText);
        final EditText date_of_birthEditText = (EditText) findViewById(R.id.date_of_birthEditText);
        final Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
        // final EditText genderEditText = (EditText) findViewById(R.id.genderEditText);
        final EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
        final EditText confirm_passwordEditText = (EditText) findViewById(R.id.confirm_passwordEditText);
        final Button terms_and_conditionsButton = (Button) findViewById(R.id.terms_and_conditionsButton);
        final Button private_policyButton = (Button) findViewById(R.id.private_policyButton);
        final Button SubmitButton = (Button) findViewById(R.id.SubmitButton);

        terms_and_conditionsButton.setOnClickListener(this);
        private_policyButton.setOnClickListener(this);
        //  ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.gender,android.R.layout.simple_spinner_item);
        //  genderSpinner.setAdapter(adapter);
        //  genderSpinner.setOnItemSelectedListener(this);
        // SubmitButton.setOnClickListener(this);
        // String gender = genderSpinner.getSelectedItem().toString();

        //   ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.genderSpinner, android.R.layout.simple_spinner_item);
        //   Gender.setAdapter(adapter);

        SubmitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String first_name = first_nameEditText.getText().toString();
                final String middle_name = middle_nameEditText.getText().toString();
                final String last_name = last_nameEditText.getText().toString();
                final String e_mail = e_mailEditText.getText().toString();
                final String user_name = user_nameEditText.getText().toString();
                final String date_of_birth = date_of_birthEditText.getText().toString();
                final String gender = genderSpinner.getSelectedItem().toString();
                final String password = passwordEditText.getText().toString();
                final String confirm_password = confirm_passwordEditText.getText().toString();


                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (success) {
                                Intent intent = new Intent(create_account.this, login.class);
                                create_account.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(create_account.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(first_name, middle_name, last_name, e_mail, user_name, date_of_birth, gender, password, confirm_password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(create_account.this);
                queue.add(registerRequest);
            }
        });
    }

    @Override
    public void onClick(View v) {




        switch (v.getId()) {
            case R.id.terms_and_conditionsButton:
                break;
            case R.id.private_policyButton:
                break;
            //     case R.id.SubmitButton:


            //        String firstName = first_nameEditText.getText().toString();
            //        String middleName = middle_nameEditText.getText().toString();
            //        String lastName = last_nameEditText.getText().toString();
            //        String Email = e_mailEditText.getText().toString();
            //        String userName = user_nameEditText.getText().toString();
            //        String dateOfBirth = date_of_birthEditText.getText().toString();
            //        String Gender = genderSpinner.getSelectedItem().toString();
            //        String Password = passwordEditText.getText().toString();
            //        String confirmPassword = confirm_passwordEditText.getText().toString();
            //        String method = "register";
            //        BackgroundTask backgroundTask = new BackgroundTask(this);
            //        backgroundTask.execute(method,firstName,middleName,lastName,Email,userName,dateOfBirth,Gender,Password,confirmPassword);


            //                User user = new User(first_name, middle_name, last_name, e_mail, user_name, date_of_birth,
            //                    gender, password, confirm_password);


        }


             }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }






            //  public void onStart(){
            //      super.onStart();

            //     EditText new_date_of_birthEditText=(EditText)findViewById(R.id.new_date_of_birthEditText);
            //      new_date_of_birthEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            //          public void onFocusChange(View view, boolean hasfocus) {
            //              if (hasfocus) {
            //                   DateOfBirthPicker dialog = new DateOfBirthPicker(view);
            //                   FragmentTransaction ft = getFragmentManager().beginTransaction();
            //                   dialog.show(ft, "DatePicker");
            //               }
            //           }
            //       });
            //   }

};


package com.example.carldwalters.communications;

import android.widget.RadioButton;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Carl on 5/10/2016.
 */
public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://127.0.0.1/register.php";
    private Map<String, String> params;

    public RegisterRequest(String first_name, String middle_name, String last_name, String e_mail, String user_name, String date_of_birth, String gender, String password, String confirm_password, Response.Listener<String> listener) {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("first_name", first_name);
        params.put("middle_name", middle_name);
        params.put("last_name", last_name);
        params.put("e_mail", e_mail);
        params.put("user_name", user_name);
        params.put("date_of_birth", date_of_birth);
        params.put("gender", gender);
        params.put("password", password);
        params.put("confirm_password", confirm_password);

    }



    @Override
    public Map<String, String> getParams() {

        return params;
    }
}


What I have tried:

I tried Googling it going on YouTube looking at forum sites all kinds of things.
Posted
Comments
Richard MacCutchan 9-Jun-16 3:41am    
Very interesting but what is the problem and where does it occur? Please do not just dump all your code and expect someone to debug it for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900