gpt4 book ai didi

java - 旋转器不起作用 setOnItemSelectedListener

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:20 27 4
gpt4 key购买 nike

当我在 spinner 中选择数据时,

Onitemselectedlistener 不起作用。不知道怎么回事?

我选择的数据不可读。如何读取我在 spinner 中选择的数据?此版本的代码不起作用,所选数据不会出现。

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, View.OnTouchListener {
private Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner = (Spinner) findViewById(R.id.spinnerMain);
ArrayAdapter < String > adapterSpinner = new ArrayAdapter < String > (MainActivity.this,
android.R.layout.simple_spinner_item, Rooms);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(MainActivity.this);
adapterSpinner.notifyDataSetChanged();

final AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView < ? > adapterView, View view, int i, long l) {
switch (i) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}

@Override
public void onNothingSelected(AdapterView < ? > adapterView) {

}
};

final Spinner spnLocale = (Spinner) findViewById(R.id.spinnerMain);

spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Selected", Toast.LENGTH_SHORT).show();
spnLocale.setOnItemSelectedListener(listener);

}

public void onNothingSelected(AdapterView < ? > adapterView) {
return;
}
});
// ...
}
// ...
}

最佳答案

您已经获得了顶部的全局旋转器。像这样实现监听器 -

spinner.setOnItemSelectedListener(new 
AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Selected", Toast.LENGTH_SHORT).show();
spinner.setOnItemSelectedListener(listener);

}

public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});

关于java - 旋转器不起作用 setOnItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56818468/

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