简体   繁体   中英

how to fix spinner that only recognize values of first item?

i have a spinner in my activity whenever i click the item it shows the values in the item i choose. but everytime i choose another item, it shows only the same value of the first item. how to fix this?? can somebody help me?

here is my code

MainActivity.java

Spinner spinnerk = (Spinner)findViewById(R.id.spinnercc);
String strk = spinnerk.getSelectedItem().toString();

if(strk.equals("A"))
    {
        k2 = 0.5;
    }
    else if(strk.equals("B"))
    {
        k2 = 0.7;
    }
    else if(strk.equals("C"))
    {
        k2 = 1;
    }
    else if(strk.equals("D"))
    {
        k2 = 1;
    }
    else if(strk.equals("E"))
    {
        k2 = 2;
    }
    else if(strk.equals("F"))
    {
        k2 = 2;
    }

in my spinner in my main.xml

<Spinner
                     android:id="@+id/spinnercc"
                     android:layout_width="85dp"
                     android:layout_height="40dp"
                     android:layout_marginTop="5dp"
                     android:background="@drawable/button_section"
                     android:entries="@array/K" />

and i already assign the string array in string.xml.

i hope this will helps you.

 final Spinner spinner_contact = (Spinner)               findViewById(R.id.spinner_contact);
    ArrayAdapter<String> adapter2;

    adapter2 = new ArrayAdapter<String>(getApplicationContext(),
            R.layout.spinner_contact, list);
    adapter2.setDropDownViewResource(R.layout.spinnerchecked);
    spinner_contact.setAdapter(adapter2);
    spinner_contact.setBackgroundResource(R.drawable.spinneredittext);

    spinner_contact
            .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> adapterView,
                        View view, int i, long l) {
                    cu_sp = list.get(i);
                     Toast.makeText(getApplicationContext(), cu_sp,
                     Toast.LENGTH_SHORT).show();
                }

                // System.out.println("vvvv selected"+myArraySpinner.get(0));
                public void onNothingSelected(AdapterView<?> adapterView) {
                    return;
                }
            });

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