Populating spinner directly in the layout xml

前端 未结 3 1386
情深已故
情深已故 2020-11-30 17:24

Is it possible to populate the options of a Spinner right in the layout xml? This page suggests I should use an ArrayAdapter? It seems awkward not being able to do it..

相关标签:
3条回答
  • 2020-11-30 18:02

    In regards to the first comment: If you do this you will get an error(in Android Studio). This is in regards to it being out of the Android namespace. If you don't know how to fix this error, check the example out below. Hope this helps!

    Example -Before :

    <string-array name="roomSize">
        <item>Small(0-4)</item>
        <item>Medium(4-8)</item>
        <item>Large(9+)</item>
    </string-array>
    

    Example - After:

    <string-array android:name="roomSize">
        <item>Small(0-4)</item>
        <item>Medium(4-8)</item>
        <item>Large(9+)</item>
    </string-array>
    
    0 讨论(0)
  • 2020-11-30 18:09

    Define this in your String.xml file and name the array what you want, such as "Weight"

    <string-array name="Weight">
    <item>Kg</item>
    <item>Gram</item>
    <item>Tons</item>
    </string-array>
    

    and this code in your layout.xml

    <Spinner 
            android:id="@+id/fromspin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/Weight"
     />
    

    In your java file, getActivity is used in fragment; if you write that code in activity, then remove getActivity.

    a = (Spinner) findViewById(R.id.fromspin);
    
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
                    R.array.weight, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            a.setAdapter(adapter);
            a.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    if (a.getSelectedItem().toString().trim().equals("Kilogram")) {
                        if (!b.getText().toString().isEmpty()) {
                            float value1 = Float.parseFloat(b.getText().toString());
                            float kg = value1;
                            c.setText(Float.toString(kg));
                            float gram = value1 * 1000;
                            d.setText(Float.toString(gram));
                            float carat = value1 * 5000;
                            e.setText(Float.toString(carat));
                            float ton = value1 / 908;
                            f.setText(Float.toString(ton));
                        }
    
                    }
    
    
    
                public void onNothingSelected(AdapterView<?> parent) {
                    // Another interface callback
                }
            });
            // Inflate the layout for this fragment
            return v;
        }
    
    0 讨论(0)
  • 2020-11-30 18:13

    I'm not sure about this, but give it a shot.

    In your strings.xml define:

    <string-array name="array_name">
    <item>Array Item One</item>
    <item>Array Item Two</item>
    <item>Array Item Three</item>
    </string-array>
    

    In your layout:

    <Spinner 
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:entries="@array/array_name"
        />
    

    I've heard this doesn't always work on the designer, but it compiles fine.

    0 讨论(0)
提交回复
热议问题