0

我有Spinner一个 ActivityA:

cmbOpciones1 = (Spinner)findViewById(R.id.CmbOpciones1);
ArrayAdapter<?> adapter1 = ArrayAdapter.createFromResource( this, R.array.exporal , android.R.layout.simple_spinner_item);  
adapter1.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
cmbOpciones1.setAdapter(adapter1);

这是数组 XML:

<string-array name="exporal">
    <item name="1">7:00 9:00am EXPRESIÓN ORAL Y ESCRITA</item>
    <item name="2">9:00 11:00am EXPRESIÓN ORAL Y ESCRITA</item>
</string-array>

我可以使用以下代码将所选项目发送Spinner到另一个活动:

//BotonSeleccionar
BotonPasar1 = (Button)findViewById(R.id.VB1);
BotonPasar1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    //Preferences Materia 1

    SharedPreferences mypreferences1 = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor1 = mypreferences1.edit();
    editor1.putString("Culo", cmbOpciones1.getSelectedItem().toString());
    editor1.commit();

在另一个活动中,我有这段代码从以下位置接收数组的项目Spinner

//Materia 1
llegada1 = (TextView) findViewById(R.id.tv1); 
llegada2 = (TextView) findViewById(R.id.tv2); 
SharedPreferences mypreferences = getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences mypreferences1 = getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
String teamnamestring = mypreferences.getString("Culo", "no_name");
String hola = mypreferences1.getString("Culoq","no_name");
llegada1.setText(teamnamestring);
llegada2.setText(hola);

假设我在 ActivityA 上有几个Spinners,在 ActivityB 上有几个TextViews接收器。我该怎么做才能按名称选择我的数组中的一个项目?例如,如果我希望所有名称为“1”的项目都转到第一个TextView,名称为“3”的项目都转到“X” TextView,有没有办法用if语句来做到这一点?我想明确一点:我想在数组 XML 示例中通过其名称来调用一个项目。

4

1 回答 1

0

您正在寻找的是以下可以在所有 Java 字符串上调用的方法:equalsIgnoreCase(string)

带有 if 语句的示例:

    String test = "test";
    String test2 = "tEsT";
    String test3 = "tester"

    if(test.equalsIgnoreCase(test2) {
        // this will be true;
    }
    if(test3.equalsIgnoreCase(test2) {
        // this will be false;
    }
于 2012-10-11T17:28:50.380 回答