来自字符串substring的android spinner getPosition()

时间:2014-03-18 09:20:56

标签: java android android-arrayadapter android-spinner

Goodday,

我在Android应用上有5个spinner s。这些spinner必须默认为在没有值的情况下启动00或者在DB中保存的值的substring(0,2)

datasource = new Source(this); 
    datasource.open();

    List<Data> values = datasource.getAll(num);

    for (Data c: values)
    {
    d1 = c.getd1();
    d2 = c.getd2();
    d3 = c.getd3();
    d4 = c.getd4();
    d5 = c.getd5();
    }

    if (d1.isEmpty())
    {
    d1 = "00";  
    }

    if (d2.isEmpty())
    {
    d2 = "00";  
    }

    if (dev3.isEmpty())
    {
    dev3 = "00";    
    }

    if (dev4.isEmpty())
    {
    dev4 = "00";    
    }

    if (d5.isEmpty())
    {
    d5 = "00";  
    }

    Spinner location1 = (Spinner)findViewById(R.id.spind1);
    Spinner location2 = (Spinner)findViewById(R.id.spind2);
    Spinner location3 = (Spinner)findViewById(R.id.spind3);
    Spinner location4 = (Spinner)findViewById(R.id.spind4);
    Spinner location5 = (Spinner)findViewById(R.id.spind5);

    ArrayAdapter myAdap1 = (ArrayAdapter) location1.getAdapter();
    ArrayAdapter myAdap2 = (ArrayAdapter) location2.getAdapter();
    ArrayAdapter myAdap3 = (ArrayAdapter) location3.getAdapter();
    ArrayAdapter myAdap4 = (ArrayAdapter) location4.getAdapter();
    ArrayAdapter myAdap5 = (ArrayAdapter) location5.getAdapter();

    int spinnerPosition1 = myAdap1.getPosition(d1);
    int spinnerPosition2 = myAdap2.getPosition(d2);
    int spinnerPosition3 = myAdap3.getPosition(d3);
    int spinnerPosition4 = myAdap4.getPosition(d4);
    int spinnerPosition5 = myAdap5.getPosition(d5);

//set the default according to value
    location1.setSelection(spinnerPosition1);
    location2.setSelection(spinnerPosition2);
    location3.setSelection(spinnerPosition3);
    location4.setSelection(spinnerPosition4);
    location5.setSelection(spinnerPosition5);

问题是我只希望substring值获得spinner位置。 例如,d1可以是&#34; 00&#34;,&#34; 01&#34;,&#34; 02&#34;,&#34; 03&#34;保存在数据库中,但spinner数组中的值为:

&#34; 00:无&#34; &#34; 01:所有&#34; &#34; 02:半&#34; &#34; 03:季度&#34;

那么如何使用:int spinnerPosition1 = myAdap1.getPosition(d1);显然会检查&#34; 01&#34; !=&#34; 01:全部&#34;。所以我需要substring spinner字符串?有谁能够帮我?

2 个答案:

答案 0 :(得分:0)

str.substring(0, 2); 

使用子字符串作为前两个字符。

答案 1 :(得分:0)

要检查substring,请尝试以下操作:

str.subString(0,2).equalsIgnoreCase("01")

strspinner字符串。