views:

91

answers:

1

I have a custom spinner (customized for formatting). It works fine and shows the result of the selected array item. The string array with my data is called mydata[].

I want to do something with that result - I've tried hours of changes but, it seems I don't know what the container is for the selected result - it just displays automatically. The mNumber refers to a case select in a class (it's result is based on what's passed into-it).

My question(s) - refer to the * WHAT DO I USE HERE * shown in the code's last line:
1. What is the container for the result?
2. How to access and syntax it?

Here's the code:
Thank you!

**[declared in onCreate]**  
    Spinner spinner = (Spinner) findViewById(R.id.Spinner01);    
    MySpinnerAdapter adapter = new MySpinnerAdapter(this, R.layout.mspinner, R.id.text,mydata);   
    spinner.setAdapter(adapter);  

**[declared outside of onCreate]**  
    @SuppressWarnings("unchecked")  
    private class MySpinnerAdapter extends ArrayAdapter{  
    public MySpinnerAdapter(Context context, int resource, int textViewResourceId, String[] objects) {  
    super(context, resource, textViewResourceId, objects);  
    final TextView woof =(TextView)findViewById(R.id.TextView07);  
    woof.setText(String.valueOf(dogday.mNumber(*** WHAT DO I USE HERE ? ***)));  
    }  
    }  

EDITED - FOR MY RESPONSE TO COMMONSWARE (too many characters for a comment box).

Thanks. You know the result that gets displayed in a spinner (by default) when something's selected - that's the piece of data I need. I want to use it as an argument for a call.

The user selects something in the spinner - The selected item will be used to make some choices in the class method (dogDay), which takes a data argument for mNumber(data) and returns a result (just like a function).

I want to do some math calcs with the result. First, I want to display what's coming back (for now) so, I'm using the dogDay.mNumber(data) as an argument for woof.text.

My question is this, How to get the piece of data (the thing the user selected in the Spinner)? How did the spinner know what to display for my selection - that's what I want? I tried using something like getSelectedItem (or whatever the it was - I can't remember just now) but, it crashes.

Is their an easier way to custom format a spinner? (and get the data) I searched hi and low for info and found only one applicable to android 1.5/later (I want a spinner with all black background and red text - I can do it via the way shown in my code using a the custom layout).

Thanks - I got a bit long winded!

EDITED - with full code Here's a full code with the custom spinner and the call you suggested. As I mentioned, I already tried it - it only shows the first item in the list - never the selected one. I use the result of getSelectedItem as an argument for the string... I tried it from both within and outside the adapter...

package com.bt.junk;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MyMaincode extends Activity {
private static String mydata[] = {"one", "two", "three"};

int poop;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
// DECLARATIONS ------------------------------------------------       
    Spinner spinner = (Spinner) findViewById(R.id.Spinner01);

    MySpinnerAdapter adapter = new MySpinnerAdapter(
        this,R.layout.custom_spinner_row,R.id.text, mydata );

    spinner.setAdapter( adapter );

    poop = spinner.getSelectedItemPosition();

}//end onCreate ********************************************

// METHODS, CLASSES, etc ---------------------------------------
@SuppressWarnings("unchecked") //<-- I added that
private class MySpinnerAdapter extends ArrayAdapter{
    public MySpinnerAdapter(Context context, int resource,
                   int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);                  

        final TextView sayWhat = (TextView) findViewById(
                                            R.id.TextView01);
        sayWhat.setText(String.valueOf(mydata[poop]));

    }//end MySpinnerAdapter
}//end class MySpinnerAdapter
}//end activity
+1  A: 

Your question makes little sense to me. I am assuming that "I want to do something with that result" means "I want to find out the selected item's position in the array". If that assumption is correct, you can get the selected position for a Spinner by calling getSelectedItemPosition(). This will be 0 when the Spinner first appears, unless you change the position yourself.

Your code is also very strange, IMHO. The constructor of an ArrayAdapter should not be attempting to manipulate a widget.

CommonsWare
see edited, above - thanks
headscratch
@headscratch: I already answered your question, including most everything in your edit. Use `getSelectedPosition()` to get the index of the selection. Then, use your array (`mydata`) to get the data associated with that position. Note that you **cannot call this from the constructor of the adapter**, because the selection will not have been set yet, because the `Spinner` will not even have the adapter yet.
CommonsWare
Please see above full example code / comment. Thanks
headscratch
@headscratch: Delete the extra code from the adapter constructor. That will never work. You are attempting to access the value of `poop` before it has been assigned. You should not be manipulating widgets from an adapter constructor. And since that code only executes once, the value will never change, anyway. Here is an example of some code that uses the position of a `Spinner`: http://github.com/commonsguy/cw-android/tree/master/Selection/Spinner/
CommonsWare
Thank you-I do appreciate your help. Here's the thing: I am able to do spinners of the generic type and do things with the selected items - no problem. But, I want to use a custom background for the entire list. As far as I know, to do that, I need to do all the stuff you suggest I remove. Otherwise, I don't see a way to replace the default spinner layouts (my custom layouts are not valid for the default spinner). If I use the getItem...calls, in my custom spinner, it only gets the first item/position.Is there a way to make a customized graphic spinner using default spinner?
headscratch
@headscratch: "As far as I know, to do that, I need to do all the stuff you suggest I remove." Absolutely not. Please read: http://commonsware.com/Android/excerpt.pdf (while it focuses on `ListView`, the concept holds for `Spinner`). You never never never never never manipulate a widget from an adapter constructor. Period. I can only say this so many ways. "Otherwise, I don't see a way to replace the default spinner layouts (my custom layouts are not valid for the default spinner)." Override `getView()`, or supply alternative layout values as *parameters* to the constructor.
CommonsWare
@headscratch: I recommend you create a separate scrap project. Get comfortable with creating and populating custom row layouts for a `ListView`, since that's covered in more samples (such as the excerpt listed above). Then convert that scrap project to use a `Spinner` and use the same techniques (you'll also need to override `getDropDownView()` for the `Spinner`). Then, and only then, return to your main project and fix up your `Spinner`.
CommonsWare
thank you - I'll read it! And, take your advice.
headscratch
Ok - I'm convinced. And, though I downloaded the full book, I'm going to buy it (gotta have hard-copy to touch and highlight). Thank you verrry much!
headscratch