gpt4 book ai didi

java - android spinner getPosition() 从字符串子字符串

转载 作者:太空宇宙 更新时间:2023-11-04 15:01:55 24 4
gpt4 key购买 nike

再见,

我在 Android 应用程序上有 5 个 spinner。如果没有值,这些spinner必须默认为以 00 开头的旋转器,或者默认为数据库中保存的值的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 可以是保存在数据库中的“00”、“01”、“02”、“03”,但 spinner 数组中的值是:

“00:无”“01:全部”《02:一半》“03:季度”

那么我该如何使用:int spinnerPosition1 = myAdap1.getPosition(d1);它显然会检查“01”!=“01:All”。所以我需要对 spinner 字符串进行子串?有谁能够帮助我?

最佳答案

str.substring(0, 2); 

对前两个字符使用子字符串。

关于java - android spinner getPosition() 从字符串子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com