我一直试图让我的微调器逻辑改变选择更改的活动,但由于某种原因,下面的代码没有改变。 Log.d返回正确的选择,因此我知道itemSelect正在触发,但不确定为什么intent不会改变。
public class ListOwnersActivity extends Activity implements OnItemClickListener, OnClickListener {
Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_cars);
spinner = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter adapter= ArrayAdapter.createFromResource(this,R.array.domain,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
addListener();
}
public void addListener() {
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long row_id) {
if (arg0.getItemAtPosition(position).toString().equals("OwnersList")) {
Log.d("position", "position: " + position);
Intent y = new Intent(getApplicationContext(), ListOwnersActivity.class);
startActivity(y);
}
if (arg0.getItemAtPosition(position).toString().equals("CarList")) {
Log.d("position", "position: " + position);
Intent x = new Intent(getApplicationContext(), ListCarsActivity.class);
startActivity(x);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
答案 0 :(得分:0)
我不确定你的意思是'意图不改变'。您可以尝试使用else更改第二个if。
答案 1 :(得分:0)
将此代码写入onItemSelected方法,它将完美运行。
String temp = String.valueOf(spinner.getSelectedItem());
if(temp.contentEquals("OwnersList"))
{
Log.d("position", "position: " + position);
Intent y = new Intent(getApplicationContext(), ListOwnersActivity.class);
startActivity(y);
}
if(temp.contentEquals("CarList"))
{
Log.d("position", "position: " + position);
Intent x = new Intent(getApplicationContext(), ListCarsActivity.class);
startActivity(x);
}
答案 2 :(得分:0)
你能否从 onCreate()中删除 spinner.setOnItemSelectedListener(this); 行?
顺便说一下你做了onItemSelectedListener()的代码两次。
1)通过基本界面 spinner.setOnItemSelectedListener(this)
2)您添加了 addListener()方法,它还包含 setOnItemSelectedListener 。
希望它会对你有所帮助。