1

I'm attempting to create the first page of an app that, after an element from the first spinner is selected, will display a second spinner and populate it with an array based on the selected element of the first spinner. I've found information regarding toArray() but I am in need of an explanation. Here is what is done so far.

package com.example.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements OnItemSelectedListener{

Spinner state_dropdown, city_dc_dropdown, city_md_dropdown,     city_va_dropdown, city_ny_dropdown, category_dropdown;
Button search_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    state_dropdown = (Spinner)findViewById(R.id.state);
    city_dc_dropdown = (Spinner)findViewById(R.id.dc_city);
    city_md_dropdown = (Spinner)findViewById(R.id.city_md);
    city_va_dropdown = (Spinner)findViewById(R.id.city_va);
    category_dropdown = (Spinner)findViewById(R.id.category);
    search_button = (Button)findViewById(R.id.search);
    state_dropdown.setOnItemClickListener(this);
}


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    String state_selected = String.valueOf(state_dropdown.getSelectedItem());
    Toast.makeText(this, state_selected, Toast.LENGTH_LONG).show();
    if(state_selected.contentEquals("DC")){
        List list = new ArrayList();

    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}

I've stopped at List because I'm so confused.

1
  • What are the values of your spinners? At least the first one? Commented Oct 27, 2015 at 22:15

2 Answers 2

3

First you have to load the array from xml and create an array list, in this way :

String[]  array_name= getResources().getStringArray(R.array.your_array);
ArrayList<String> namesList= new ArrayList<String>(Arrays.asList(array_name)));

and when you select the right item populate the spinner:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.your_l, namesList);
yourSpinner.setAdapter(adapter);
Sign up to request clarification or add additional context in comments.

Comments

0

I feel like this is double work. You already have the array in arrays.xml. Why make it again as a list. That's more data to process and more stress for the pc. The best option would be to set the adapter to pick directly from arrays.xml and populate the spinner.

The array data is inserted during the create from resource method, and in the on click, all you have to do is assign the string value of whatever has been clicked on to a predefined string variable.

1 Comment

Can you edit your answer to include a code sample? That would really round out this guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.