basic spinner example

后端 未结 4 1157
醉话见心
醉话见心 2020-12-16 20:40

Main.xml




        
相关标签:
4条回答
  • 2020-12-16 20:51

    You are calling toString() method on spinner to get the selection which is wrong. You need to call getSelectedItemPosition() method to get the selection.

    0 讨论(0)
  • 2020-12-16 20:56
    b.setOnClickListener(new OnClickListener()
        {
    
            public void onClick(View arg0)
            {
                String spin=s.getSelectedItem().toString();
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                tv.setText(spin);
            }
        });
    
    0 讨论(0)
  • 2020-12-16 21:04
        spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
             public void onItemSelected(AdapterView<?> parentView,
                     View selectedItemView, int position, long id) {
                 try {
    
                      String select_item =parentView.getItemAtPosition(position).toString();
                     } 
               catch (Exception e) {
    
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
    
            }
    
        });
    
    0 讨论(0)
  • 2020-12-16 21:08

    You dont need shared preference to load values in spinner. You just need to declare array in string.xml file and load that.I am giving you my code.Just use it.:-

    STEP-1:-

    Declare array for spinner in your string.xml(res->values->strings.xml):--

    <string-array name="country_array">
        <item>Greece</item>
        <item>United Kingdom</item>
        <item>Italy</item>
        <item>France</item>
        <item>Germany</item>
        <item>Turkey</item>
        <item>Poland</item>
        <item>India</item>
    </string-array>
    

    STEP-2:-

    Declare Spinner widget in your layout xml file

    <Spinner
         android:id="@+id/spinCountry"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp"
         android:paddingLeft="8dp"
         android:popupBackground="@android:color/white"
         android:scrollbars="none"
         android:spinnerMode="dropdown" />
    

    STEP-3:-

    Declare Spinner in your Activity

    Spinner spinCountry;
    spinCountry= (Spinner) findViewById(R.id.spinCountry);//fetch the spinner from layout file
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, getResources()
                        .getStringArray(R.array.country_array));//setting the country_array to spinner
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinCountry.setAdapter(adapter);
    //if you want to set any action you can do in this listener
    spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    
    0 讨论(0)
提交回复
热议问题