我在我的android應用程序中有一個微調控件,我希望用戶從微調框中選擇一個選項。一旦選擇該選項,用戶可以被重定向到另一個意圖。我的問題是如何從spinner的onitemselcted方法調用意圖?我該如何從spinners調用意圖onitemSelected方法
或者我正在絞擰還有其他方法可以做到這一點。我需要用戶先從下拉菜單中設置選項,然後才能繼續下一頁。
如果我把startactivity(意向),在我的onitemselected方法我得到這個 錯誤的方法startActivity(意向)是未定義的 型MyOnItemSelectedListener(Myonitemselectedlistner是我的類 器具OnItemSelectedListener)
這是我onitemslectedlistner代碼
class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Context ctx = view.getContext();
SharedPreferences myPrefs = ctx.getSharedPreferences("hello", android.content.Context.MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("city", parent.getItemAtPosition(pos).toString());
prefsEditor.commit();
Intent intent = new Intent();
intent.setClass(ctx, MainActivity.class);
startActivity(intent);
}
public void onNothingSelected(AdapterView parent) {
//do nithong
}
}
ü可以發佈您的Spinner OnItemSelcted方法的代碼。 – Venky
添加請參閱 –
而不是Intent intent = new Intent(); intent.setClass(ctx,MainActivity.class); startActivity(intent); 更改爲startActivity(新的意圖(Classnmae.this,Main.class)) – Venky