-2

i'm practising how to add an string to the addapter of the spinner. If i declare the string in the java activity, works perfectly, but if i do in the string.xml:

<string-array name="tabs">
    <item>tab</item>
    <item>tab1</item>
    <item>tab2</item>
    <item>tab3</item>
    <item>tab4</item>
    <item>tab5</item>
</string-array>

and in the java

ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs);

dont work.

what's the problem? Thanks

This is the full code, because i have problems whit the app (crash)

public class MainActivity extends AppCompatActivity {

    Spinner OptionSpinner;
    TextView textview;


    String[] stabs= getResources().getStringArray(R.array.tabs);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
        textview = (TextView) findViewById(R.id.textview);

        ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);

       adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        OptionSpinner.setAdapter(adapter1);

        OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                textview.setText("seleccionado: " + parent.getItemAtPosition(position));
                if(position==1){
                    Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
                    startActivity(IntentActT1);
                }
            }

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

            }
        });


    }
}
Electrocode
  • 127
  • 1
  • 8

2 Answers2

0
 String[] stabs= getResources().getStringArray(R.array.tabs);
 ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs); 

Edited 1

public class MainActivity extends AppCompatActivity {

Spinner OptionSpinner;
TextView textview;
String[] stabs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
    textview = (TextView) findViewById(R.id.textview);
    stabs= getResources().getStringArray(R.array.tabs);

    ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    OptionSpinner.setAdapter(adapter1);

    OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            textview.setText("seleccionado: " + parent.getItemAtPosition(position));
            if(position==1){
                Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
                startActivity(IntentActT1);
            }
         }

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

        }
    });


 }
 }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

The constructor ArrayAdapter<String>(Context, int, int) doesn't use the third argument as an array resource id; it uses it as the id of the TextView to use to display each item in the layout. Instead, you can pass getStringArray(R.array.tabs) as the third argument (or getContext().getStringArray(R.array.tabs), if this code is not in an Activity).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521