简体   繁体   中英

How to retrieve individual selections from multiple spinners using one adapter?

I'm building a small unit conversion app that utilizes two spinners to allow for the selection of a from value and a to value. The two spinners are using the same adapter and the same string array in the strings.xml. How do I allow the spinners to be selected individually while storing the selected values into two separate variables at the same time?

I have tried using and if statement and a switch, but they are conditional and control statements which don't seem to solve the problem. The spinners could be selected individually, which is what I want. However, I could not figure how to pass the variables that contained the values out of the method.

I have also tried call getSelectedItem() method on both variables, however it doesn't seem like the method can distinguish between the spinners in this way. Aside from that, it nearly works exactly as I intended, the variables hold the selected spinner value.
I also had to set the spinner position using this setSelection() due to the spinners being null at the start and crashing the app.

Adapter with Spinners: The spinnerFrom and spinnerTo variables are global.

spinnerFrom = (Spinner) findViewById(R.id.spinnerDropdown);
spinnerTo = (Spinner) findViewById(R.id.spinnerDropdown2);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.velocity_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

        spinnerFrom.setAdapter(adapter);
        spinnerTo.setAdapter(adapter);
        spinnerFrom.setOnItemSelectedListener(this);
        spinnerTo.setOnItemSelectedListener(this);

If-statement(First Attempt)

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
        if(parent.getId() == R.id.spinnerDropdown)
        {
            spinner1 = Integer.parseInt(parent.getItemAtPosition(position).toString());
        }
        else if(parent.getId() == R.id.spinnerDropdown2)
        {
            spinner2 = Integer.parseInt(parent.getItemAtPosition(position).toString());
        }
}

getSelectedItem()(Second Attempt)

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
        spinnerFrom.setSelection(position);
        spinnerTo.setSelection(position);

        String values = "Spinner 1: " + spinnerFrom.getSelectedItem().toString() +
                "\nSpinner 2: " + spinnerTo.getSelectedItem().toString();

        resultDisplay.setText(values);
}

Update: Current code

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        if(parent.getId() == R.id.spinnerDropdown)
        {
            spinner1 =(xmlArray[position]);
        }
        else if(parent.getId() == R.id.spinnerDropdown2)
        {
            spinner2 =(xmlArray[position]);
        }

        convertUnits(spinner1, spinner2); <-- Line 82

 public void convertUnits(String spinner1, String spinner2)
    {
        if(spinner1.contains("Double") && spinner2.contains("Triple")) <-- Line 95
        {
           //conversion method
        }
        ...

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
        at com.example.courseproject.Velocity.convertUnits(Velocity.java:95)
        at com.example.courseproject.Velocity.onItemSelected(Velocity.java:82)

Edit: The spinners work, but this was my original problem with using a conditional if-statement. The spinner2 variable is null and my app crashes. I need both variables. I have tried both .equals and .contains, neither resolve the nullpointer.

Edit2: I can't copy/paste what the debugger is showing me, but at line 95 spinner2 is showing as 'null'. It also appears like the value of spinner2 never changes from the starting null anywhere in the code while spinner1 does.

Follow the below code for better view to solve your problem:

    public class MainActivity extends AppCompatActivity implements 
                  AdapterView.OnItemSelectedListener {
              Spinner spinnerFrom,spinnerTo;
              String[] menuArray;


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                TextView textView=findViewById(R.id.text);

                spinnerFrom = (Spinner) findViewById(R.id.spinnerDropdown);
                spinnerTo = (Spinner) findViewById(R.id.spinnerDropdown2);

                menuArray = getResources().getStringArray(R.array.velocity_array);


                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                        R.array.velocity_array, android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

                spinnerFrom.setAdapter(adapter);
                spinnerTo.setAdapter(adapter);
                spinnerFrom.setOnItemSelectedListener(this);
                spinnerTo.setOnItemSelectedListener(this);
            }

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                if(parent.getId() == R.id.spinnerDropdown)
                {
                  int  spinner1 = Integer.parseInt(menuArray[position]);
                }
                else if(parent.getId() == R.id.spinnerDropdown2)
                {
                  int  spinner2 = Integer.parseInt(menuArray[position]);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        }

<string-array name="velocity_array">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM