Saransh Agarwal Saransh Agarwal - 6 months ago 48
Android Question

Displaying data according to the item in the spinner selected in another spinner?

I have made a program to select a state and according to the selected state it display the cities inside it but the cities are not displayed please help

This is the MainActivity

public class MainActivity extends AppCompatActivity {
Spinner state, city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
state= (Spinner) findViewById(R.id.spinner);
city= (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter= ArrayAdapter.createFromResource(this,R.array.states,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String state= parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "State = "+state, Toast.LENGTH_SHORT).show();
city= (Spinner) findViewById(R.id.spinner2);
if (state=="Rajasthan"){
ArrayAdapter<CharSequence> adapter1= ArrayAdapter.createFromResource(getApplicationContext(),R.array.cities_rajasthan,android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(adapter1);

}
else if (state=="Madhya Pradesh"){
ArrayAdapter<CharSequence> adapter2= ArrayAdapter.createFromResource(getApplicationContext(),R.array.cities_madhyapradesh,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(adapter2);
}
else if (state=="Uttar Pradesh"){
ArrayAdapter<CharSequence> adapter3= ArrayAdapter.createFromResource(getApplicationContext(),R.array.cities_uttarpradesh,android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(adapter3);
}
else if (state=="Maharashtra"){
ArrayAdapter<CharSequence> adapter4= ArrayAdapter.createFromResource(getApplicationContext(),R.array.cities_maharashtra,android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(adapter4);
}

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(MainActivity.this, "Nothing is Selected ", Toast.LENGTH_SHORT).show();
}
});
}}


Here is the String.xml file

<resources>
<string name="app_name">Spinners2</string>
<string-array name="states">
<item>Rajasthan</item>
<item>Madhya Pradesh</item>
<item>Maharashtra</item>
<item>Uttar Pradesh</item>
</string-array>
<string-array name="cities_rajasthan">
<item>Bikaner</item>
<item>Jaipur</item>
<item>Jodhpur</item>
<item>Jaisalmer</item>
</string-array>
<string-array name="cities_madhyapradesh">
<item>Indore</item>
<item>Bhopal</item>
<item>Jabalpur</item>
<item>Ujjain</item>
</string-array>
<string-array name="cities_maharashtra">
<item>Pune</item>
<item>Mumbai</item>
<item>Nagpur</item>
</string-array>
<string-array name="cities_uttarpradesh">
<item>Lucknow</item>
<item>Agra</item>
<item>Kanpur</item>
</string-array>
</resources>


This is the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context="com.example.saranshagarwal.spinners2.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="State"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City"
android:id="@+id/textView2"
android:layout_below="@+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Answer

Try like this,

state.setOnItemSelectedListener(
  new OnItemSelectedListener() {
      public void onItemSelected(
            AdapterView<?> parent, View view, int position, long id) {
                String selected_item = state.getSelectedItem().toString();

                if(selected_item.equals("Rajastan"))
                {
                    //do your operation here
                }
                elseif(..)
                {
                ..
                }
                //etc

        }

            public void onNothingSelected(AdapterView<?> parent) {
                 Toast.makeText(MainActivity.this, "Nothing is Selected ", Toast.LENGTH_SHORT).show();
            }
});

Using String.equals()

this may helps you.

Happy coding!!!!