字符串资源

时间:2016-02-19 05:51:43

标签: android arrays string

我正在练习如何将字符串添加到微调器的addapter中。如果我在java活动中声明字符串,那么效果很好,但是如果我在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>

并在java

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

不行。

有什么问题?感谢

这是完整的代码,因为我在应用程序中遇到问题(崩溃)

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) {

            }
        });


    }
}

2 个答案:

答案 0 :(得分:0)

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

已编辑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) {

        }
    });


 }
 }

答案 1 :(得分:0)

构造函数ArrayAdapter<String>(Context, int, int)没有使用第三个参数作为数组资源id;它使用它作为TextView的id来用于显示布局中的每个项目。相反,您可以将getStringArray(R.array.tabs)作为第三个参数传递(或getContext().getStringArray(R.array.tabs),如果此代码不在Activity中)。