1)在我的活动中,我有两个旋转器:countrySpinner,citySpinner。第二个旋转器依赖于第一个--如果用户从countrySpinner中选择项目A,那么第二个citySpinner中的值也将被更新,例如,如果用户从第一个citySpinner中选择俄罗斯,他将得到俄罗斯城市.
<-有效。
2)在我的活动中,我有一个按钮,它保存用户在纺丝器中提供的信息。信息存储在共享的应用程序首选项中。我没有在代码示例中包括这一点,因为它与此无关。
<-它起作用
3.在活动的onCreate方法中,我读取了先前保存的值-> checkIfLocalSettingExists(),并将保存的值赋值给'countryStr‘和'cityStr’变量。
<-它起作用
4.然后,我想更新一开始就有默认值的自旋器。我有存储在共享首选项中的字符串,因此为了获得正确的元素,我使用getIndex函数,它返回应该设置的元素的id。
<-资讯科技工程
5.在我得到需要在两个旋转器上设置的元素之后,我可以只设置它们。我将检索到的is分配给旋转器,但是,setOnItemSelectedListener被执行了两次,不知道原因,这可能是它不能工作的原因!
<-它不工作!
代码:
public class Settings extends ActionBarActivity{
public Spinner countrySpinner;
public Spinner citySpinner;
public String cityStr;
public String countryStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_settings);
countrySpinner = (Spinner) findViewById(R.id.kraje_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Countries, R.layout.spinner_layout);
countrySpinner.setAdapter(adapter);
citySpinner = (Spinner) findViewById(R.id.przejscie_spinner);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesRussia, R.layout.spinner_layout);
citySpinner.setAdapter(adapter2);
if (checkIfLocalSettingExists())
{
System.out.println("Gathered following values from settings");
System.out.println("Country: " + countryStr);
System.out.println("City: " + cityStr);
System.out.println("----");
System.out.println("Setting the spinners...");
System.out.println("Country spinner:");
// I found the ID of saved country, I can set it in countrySpinner
countrySpinner.setSelection(getIndex(countrySpinner, countryStr));
System.out.println("City spinner:");
//without this it didn't work at all, don't know why
if (kraj.toLowerCase().equals("russia"))
adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesRussia, R.layout.spinner_layout);
else if (kraj.toLowerCase().equals("ukraine"))
adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesUkraine, R.layout.spinner_layout);
else {
adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesBelarus, R.layout.spinner_layout);
}
citySpinner.setAdapter(adapter2);
// I found the ID of saved city, I can set it in citySpinner
citySpinner.setSelection(getIndex(citySpinner, cityStr));
}
countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
System.out.println("SELECTED RUSSIA");
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Settings.this,
R.array.CitiesRussia, R.layout.spinner_layout);
adapter2.setDropDownViewResource(R.layout.spinner_layout);
citySpinner.setAdapter(adapter2);
}
//
else if (position == 1) {
System.out.println("SELECTED UKRAINE");
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Settings.this,
R.array.CitiesUkraine, R.layout.spinner_layout);
adapter2.setDropDownViewResource(R.layout.spinner_layout);
citySpinner.setAdapter(adapter2);
} else {
System.out.println("SELECTED BELARUS");
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Settings.this,
R.array.CitiesRussia, R.layout.spinner_layout);
adapter2.setDropDownViewResource(R.layout.spinner_layout);
citySpinner.setAdapter(adapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private int getIndex(Spinner spin, String myString){
int index = 0;
String tmp = "";
System.out.println("Im in get index function!");
System.out.println("Seraching for: " + myString);
for (int i=0;i<spin.getCount();i++){
tmp = spin.getItemAtPosition(i).toString();
System.out.println("checking if " + tmp.toLowerCase() + " equals " + myString.toLowerCase());
if (tmp.toLowerCase().equals(myString.toLowerCase())){
System.out.println("FOUND IT --> " +tmp + " ---->" + i +", its: " + myString);
index = i;
break;
}
else
System.out.println("NOT FOUND IT --> " +tmp + "-->" + i +", its NOT: " + myString);
}
return index;
}代码输出:
03-16 15:51:12.590 I/System.out: Gathered following values from settings
03-16 15:51:12.590 I/System.out: Country: Belarus
03-16 15:51:12.590 I/System.out: City: Terespol
03-16 15:51:12.590 I/System.out: ----
03-16 15:51:12.590 I/System.out: Setting the spinners...
03-16 15:51:12.590 I/System.out: Country spinner:
03-16 15:51:12.590 I/System.out: Im in get index function!
03-16 15:51:12.590 I/System.out: Seraching for: Belarus
03-16 15:51:12.590 I/System.out: checking if russia equals belarus
03-16 15:51:12.590 I/System.out: NOT FOUND IT --> russia -->0, its NOT: Belarus
03-16 15:51:12.590 I/System.out: checking if ukraine equals belarus
03-16 15:51:12.590 I/System.out: NOT FOUND IT --> ukraine-->1, its NOT: Belarus
03-16 15:51:12.590 I/System.out: checking if belarus equals belarus
03-16 15:51:12.590 I/System.out: FOUND IT --> Belarus ---->2, its: Belarus
03-16 15:51:12.590 I/System.out: City spinner:
03-16 15:51:12.590 I/System.out: Im in get index function!
03-16 15:51:12.590 I/System.out: Seraching for: Terespol
03-16 15:51:12.590 I/System.out: checking if cityA equals terespol
03-16 15:51:12.590 I/System.out: NOT FOUND IT --> cityA -->0, its NOT: Terespol
03-16 15:51:12.590 I/System.out: checking if cityB equals terespol
03-16 15:51:12.590 I/System.out: NOT FOUND IT --> cityB -->1, its NOT: Terespol
03-16 15:51:12.590 I/System.out: checking if cityC equals terespol
03-16 15:51:12.594 I/System.out: NOT FOUND IT --> cityC -->2, its NOT: Terespol
03-16 15:51:12.594 I/System.out: checking if cityD equals terespol
03-16 15:51:12.594 I/System.out: NOT FOUND IT --> cityD -->3, its NOT: Terespol
03-16 15:51:12.594 I/System.out: checking if cityE equals terespol
03-16 15:51:12.594 I/System.out: NOT FOUND IT --> cityE -->4, its NOT: Terespol
03-16 15:51:12.594 I/System.out: checking if terespol equals terespol
03-16 15:51:12.594 I/System.out: FOUND IT --> Terespol ---->5, its: Terespol
03-16 15:51:12.594 I/System.out: 5
03-16 15:51:12.642 I/System.out: SELECTED BELARUS因此,白俄罗斯的代码是2,它由getIndex函数正确返回,然后旋转器被设置为有效值(白俄罗斯)。Terespol的返回代码为5,它由getIndex函数正确地返回,然后由于setOnItemSelectedListener是为countrySpinner执行的,所以旋转器的设置不正确。再次(检查输出的最后一行)。为什么?我只将侦听器设置为countrySpinner,而不是citySpinner?!因此,citySpinner不是设置为第5元素,而是设置为第1元素。为什么在这个世界上它不工作!?
发布于 2016-03-16 17:34:34
无论何时您从旋转器中手动选择任何项,或者像这样调用setSelection() -
countrySpinner.setSelection(getIndex(countrySpinner, countryStr));它一定会调用onItemSelected(),并且在onItemSelected()中,您正在加载城市旋转值,这会重置您的选择,尽管您之前选择了一个特定的项。
要解决这个问题,您必须跟踪是否从checkIfLocalSettingExists()加载了国家旋转器值,在这种情况下,不必在onItemSelected()中设置城市旋转适配器,因为您已经在这样做了。
发布于 2016-03-16 19:47:44
我通过设置额外的监听器来做到这一点,在其中我将allowModification标志设置为true。仍然不知道为什么以前它没有工作,为什么监听器总是在最后被执行(bug?)
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
allowModification = true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});https://stackoverflow.com/questions/36042549
复制相似问题