Dynamically set the contents of one counter based on the selected value of another counter of another class

I created two players for different classes. I want to set the contents of one counter based on the selected value of another counter that has in another class. Tell someone can help me. Thanks in advance.

+3
source share
2 answers

There is a solution, you can feel that it is long, but it works great for you, I think!

Lets say you have spinner_one in your ActivityOne.class and spinner_two in 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
}

spinner_2 onResume() ActivityTwo.class, , spinner_1.

spinner_2 spinner_1 .

+1

, - ArrayAdapter Spinner, / ArrayAdapter.

, , OnItemSelectedListener.onItemSelected(), , , Spinner.getSelectedItemPosition(), , ArrayAdapter, .

Spinners, , , . 95% , , , - Spinner , , . , setSelection() onItemSelected(), setSelection() .

:

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 );
            }
        }
    }

}

setSpinner *(), , onSelectedItemListener() . , , . ( - onItemSelected() .)

, .

+1

All Articles