user1576339
user1576339

Reputation: 317

Android - Dynamically Adding values to Spinners

I am new to Android Development so the query may seem novice.

I was trying to make an app where there are a couple of spinners and after the user selects one spinner the other spinner gets filled up with the data based on the selection in spinner1.

ie. First user selects Country. Based on country the spinner 2 gets filled up with states and based on the state the spinner 3 gets filled up with cities.

I have created all the countries as a string arrays in strings.xml like -

<string-array name="Country">
    <item >USA</item> etc...

Similarly states have been created in the string.xml as - (Each Country is a separate entry with a naming convention as (country name)_(States)

<string-array name="USA_state">
    <item >New York</item> etc...

Now I want the second spinner to populate based in country selection. Hence the formula used by me is to the get the second spinner is -

String getstate = country[index]+"_"+"state";
state=getResources()
.getStringArray(getResources().
getIdentifier(getstate,null,getPackageName()));

When I run this app the Manactivity just shows a blank screen and I donot get any Debug info and get an error "the jar file android.jar has no source attached".

Please help. The complete mainActivity is given below -

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.view.View; 

public class MainActivity extends Activity {
String[] country;
String[] state;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    country = getResources().getStringArray(R.array.country); 
    Spinner country_spinner =(Spinner) findViewById(R.id.country); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
    country_spinner.setAdapter(adapter); 
    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int index = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You have Selected"+country[index]+"country", Toast.LENGTH_SHORT).show();
            Fill (index);
        }
        public void onNothingSelected(AdapterView<?> arg0) {}
    });


}

protected void Fill(int index) {
    // TODO Auto-generated method stub
    //Field resField=R.array.getField(country[index]);
    //int getstate = resField.getInt(null);
    String getstate = country[index]+"_"+"state";
    state=getResources().getStringArray(getResources().getIdentifier(getstate,null,getPackageName()));
    Spinner state_spinner = (Spinner) findViewById(R.id.state);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state);
    state_spinner.setAdapter(adapter);
    state_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int index2 = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(),"You Have Selected "+state[index2] + " statet",Toast.LENGTH_SHORT).show();
        }
        public void onNothingSelected(AdapterView<?> arg0){}
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

Upvotes: 1

Views: 2542

Answers (1)

user1576339
user1576339

Reputation: 317

I was able to solve the problem. The final code is given below. This is a nested approach so if there are 3 spinners (like city after state) do we again nest the code for that spinner? Is there any simpler method?

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
    country_spinner.setAdapter(adapter); 
    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int index = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You have Selected"+country[index]+" County", Toast.LENGTH_SHORT).show();

            final CharSequence[] state_array = state.getTextArray(index);
            final Spinner state_spinner =(Spinner) findViewById(R.id.state); 
            ArrayAdapter<CharSequence> adapter1 = new ArrayAdapter<CharSequence>(MainActivity.this, android.R.layout.simple_spinner_item, state_array);
            state_spinner.setAdapter(adapter1); 
            state_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

                public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){
                    final int index = arg0.getSelectedItemPosition();
                    Toast.makeText(getBaseContext(), "You have Selected"+state_array[index]+" State", Toast.LENGTH_SHORT).show();

        });

                }
                public void onNothingSelected(AdapterView<?> arg0) {
                    state_spinner.setAdapter(null);
                }
            });

Upvotes: 2

Related Questions