java – spinner is returning null value

I have created two dependent spinner that is one is state spinner and second is city spinner which shows cities depending upon the state selected. After selecting the state the city spinner drop down list is showing correct city names but I don’t know why the city spinner is not showing any text in the text area of ​​spinner. Drop down list is perfect but the value selected is not showing up in the spinner and that is why the city name is showing null value . Below is the spinner code

changeLocation.setOnClickListener(new View.OnClickListener() {

        String states;
        String cities;
        @Override
        public void onClick(View v) {
            dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.sessiondialog);
            
      dialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.dialogback));
            


         dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.
            LayoutParams.WRAP_CONTENT);
            dialog.setCancelable(false);

            dialog.show();

            stateView = dialog.findViewById(R.id.stateSpinner);
            cityView = dialog.findViewById(R.id.citySpinner);
            go = dialog.findViewById(R.id.dialogGo);

            ArrayAdapter<String> myAdapter2 = new  ArrayAdapter(MainActivity.this,  
             R.layout.sampledroptextlayout,stateList);
            stateView.setAdapter(myAdapter2);

            stateView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
        {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int 
         position, long id) {


                    states = stateView.getSelectedItem().toString().trim();

                    citList = getCity(states);
                    ArrayAdapter<String> myAdapter3 = new ArrayAdapter<String> 
         (parent.getContext(), R.layout.sampledroptextlayout, citList);
                    cityView.setAdapter(myAdapter3);
                    myAdapter3.notifyDataSetChanged();



                    cityView.setOnItemSelectedListener(new 
       AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int 
          position, long id) {

                            cities = parent.getSelectedItem().toString();

                            

                        }

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

                        }
                    });

                }


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

                }
            });

            go.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
           

                    if(cities.trim().equalsIgnoreCase("")|| 
             states.trim().equalsIgnoreCase("")){
                        Toast.makeText(MainActivity.this, "Fill up all required feilds", 
            Toast.LENGTH_LONG).show();
                    }else {
                        locationSession session = new 
              locationSession(MainActivity.this);
                        session.saveSession(cities, states);

                        recreate();
                    }
                }
            });

        }
    });

Below is the getCity() and getStateName() functions

  public  ArrayList<String> getCity(String stateName){
    ArrayList<String> cityList = new ArrayList<>();


    final  String state = stateName;

    StringRequest request = new StringRequest(com.android.volley.Request.Method.POST, 
       url4, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject Jobject = new 
               JSONObject(response.substring(response.indexOf("{"), 
                response.lastIndexOf("}") + 1));
                String success = Jobject.getString("success");
                JSONArray Jarray = Jobject.getJSONArray("datas");
                if(success.equals("1")) {
                    for (int i = 0; i < Jarray.length(); i++) {
                        JSONObject object = Jarray.getJSONObject(i);
                        String cityText = object.getString("city");
                        int id = object.getInt("id");

                        cityList.add(cityText);
                    }
                }

            } catch (JSONException jsonException) {
                jsonException.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }


    })
    {
        @Override
        protected Map<String, String> getParams() {
            Map<String,String> map=new HashMap<String, String>();
            map.put("state",state);
            return map;
        }
    };

    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    queue.add(request);

    return cityList;
};



 public void getStateName(){
   // ArrayList<frontCategoryModal> catArray = new ArrayList<>();

    StringRequest request = new StringRequest(com.android.volley.Request.Method.POST, 
   url5, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject Jobject = new 
    JSONObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 
        1));
                String success = Jobject.getString("success");
                JSONArray Jarray = Jobject.getJSONArray("datas");
                if(success.equals("1")) {
                    for (int i = 0; i < Jarray.length(); i++) {
                        JSONObject object = Jarray.getJSONObject(i);
                        String stateText = object.getString("state");
                        int id = object.getInt("id");

                        stateList.add(stateText);
                    }
                }

            } catch (JSONException jsonException) {
                jsonException.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }

    });

    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    queue.add(request);
};

Below is the xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:padding="10dp">

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="5dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:srcCompat="@drawable/placeholder" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Your city and state name is required."
        android:textColor="@color/black"
        android:textSize="16sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout2">


    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Select state"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <Spinner
        android:id="@+id/stateSpinner"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/blackborders"
        tools:ignore="SpeakableTextPresentCheck" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Select City"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <Spinner
        android:id="@+id/citySpinner"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/blackborders"
        tools:ignore="SpeakableTextPresentCheck" />

    <Button
        android:id="@+id/dialogGo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:backgroundTint="@color/black"
        android:padding="12dp"
        android:text="Let's start"
        android:textColor="@color/white"
        app:icon="@drawable/shuttle"
        app:iconGravity="textStart"
        app:iconTint="@color/white" />
</LinearLayout>
 </androidx.constraintlayout.widget.ConstraintLayout>

I am using this in a dialog box

DroptextLayout xml file

<?xml version="1.0" encoding="utf-8"?>

 [enter image description here][1]<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/white"
android:padding="10dp"
android:text="texts"
android:textColor="@color/black"
android:textSize="16sp" />

Leave a Comment