Dynamic Spinner > Find Selection?

Put your problem here if it does not fit any of the other categories.

Dynamic Spinner > Find Selection?

Postby cerealspiller » Sun Apr 10, 2011 5:59 pm

I've got 2 spinners to populate dynamically based on two different strings. I know my format has got to be messed up, but I don't know how to move around or call activities yet (Some advice there would be nice).

What I'm trying to get the code to do, is if the user selects the first option in the career spinner (High School) to change the hobby string (Add "Chess Club"). I'll add the else statement later, once this gets fixed.

The problem with the code now is that the hobby string is local... yet it can't find it one bracket down in an if() statement. All research on Global Variables has just confused me further.

Here's the code...

Code: Select all
public class MainMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        String[] hobby = {"Video Games", "Gardening"};
       
        String[] career = {"High School", "Teacher"};
             
        Spinner cspinner = (Spinner) findViewById(R.id.cspinner);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, career);
        spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        cspinner.setAdapter(spinnerArrayAdapter);

       
        Spinner hspinner = (Spinner) findViewById(R.id.hspinner);
        ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, hobby);
        spinnerArrayAdapter1.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        hspinner.setAdapter(spinnerArrayAdapter1);
       
        cspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
           
           public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
              adapter.getItemAtPosition(i).toString();
              if (i == 0){
                 String[] hobby = {"Chess Club", "Video Games", "Gardening"};                
              }
           }
           
           public void onNothingSelected(AdapterView<?> arg0) {
           //do something else
           }
           });

    }
}
cerealspiller
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 09, 2011 4:05 pm

Top

Re: Dynamic Spinner > Find Selection?

Postby CrappyCoder » Mon Apr 11, 2011 10:59 am

Try changing your if-statement to this:

Code: Select all
if (adapter.getSelectedItem()== 0)
{
   String[] hobby = {"Chess Club", "Video Games", "Gardening"};               
}
CrappyCoder
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 10, 2011 7:48 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests