开发者

dynamically set content of one spinner based on selected value of other spinner of another class

i have created two spinners both of different c开发者_如何转开发lass.i want to set content of one spinner based on selected value of other spinner which is having in other class.can any one help me.thanks in advance.


There is a solution,you might feel it lengthy to implement but it would perfectly work for you,i think!

Lets say you have spinner_one in your ActivityOne.class and spinner_two in your ActivityTwo.class. And you want to populate spinner_2 based on what is selected in spinner_1.

Then,

spinner_1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {              

                 // save your selected item into a SharedPreference Variable
        }    
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
});

Now,for spinner_2:

String spinner1_value=get value from SharedPreference;
if(spinner1_value.equals("something_1"))
{
   //populate spinner_2 accordingly
}
else if(spinner1_value.equals("something_2"))
{
   //populate spinner_2 accordingly
}
else
{
   //populate spinner_2 accordingly
}

And don't forget to put this code to populate spinner_2 in onResume() of your ActivityTwo.class,so that it will reflect the changes user made in spinner_1 value.

This will allow you to change spinner_2 content according to spinner_1 value as and when user changes it.


The short answer is that you can do this programatically - either by assigning a new ArrayAdapter to the Spinner, or by removing/adding items from the existing ArrayAdapter.

The longer answer is that you have to be very careful in relying on OnItemSelectedListener.onItemSelected(), since it will not get called if the old selection and a new selection happen to be in the same position, and Spinner.getSelectedItemPosition(), since it can actually return positions greater than the number of items in the ArrayAdapter if you're removing items on the fly.

I have a system of three cascaded Spinners, which in turn can drive the contents of other Buttons and Texts. I was able to get 95% solutions until I recognized the above and changed my view of what was authoritative and what was not - by making the Spinner subservient to the logic that determines content, rather than the other way around. So rather than calling setSelection() and relying on the onItemSelected() callback, do all your cascading logic outside the handler, and only then call setSelection() and return back up the chain.

Adapted from my working code:

class Spinster extends Activity {
    ...
    private void setSpinnerOne( int pos ) {
        // 1. Do our own chores, etc
        doSomeStuff();
        mSomeText.setText( "Blah!" );
        mSomeButton.setEnabled( some_condition );

        // 2. Populate dependent Spinner based on pos
        populateSpinnerTwoAdapter( pos );

        // 3. Cascade by calling SpinnerTwo logic (not the Spinner itself)
        lSpinnerTwoPos = someNiceFunction();
        setSpinnerTwo( lSpinnerTwoPos );

        // 4. Now set SpinnerOne
        mSpinnerTwo.setSelection( pos );
    }

    private void setSpinnerTwo( int pos ) {
        // Follows the same pattern as setSpinnerOne(), but cascades to SpinnerThree
    }

    private void setSpinnerThree( int pos ) {
        // Follows the same pattern as setSpinnerOne(), but need not cascade
    }

    ...

    private OnItemSelectedListener item_select = new OnItemSelectedListener() {
        public void onItemSelected( AdapterView parent, View v, int position, long id )
            {
            ...
            int lId = parent.getId();

            if ( lId == R.id.spinner_one ) {
                setSpinnerOne( position );
            } else if ( lId == R.id.spinner_two ) {
                setSpinnerTwo( position );
            } else if ( lId == R.id.spinner_three ) {
                setSpinnerThree( position );
            }
        }
    }

}

I've left out a small detail here, that of setting and clearing a guard variable in the setSpinner*() methods so that they don't bother doing all their work again when onSelectedItemListener() calls them back. I think the basic logic is easier to show by leaving it out. (And they don't strictly need to be there - onItemSelected() will not be called the second time round.)

I could post a real, working example if this is deemed to abstract.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜