2017-02-11 68 views
0

我試圖讓微調對用戶輸入做出反應,但是當我按下其中一個項目時,什麼也沒有發生。我在IDE或運行時沒有收到任何錯誤,所以我不知道我做錯了什麼。誰能幫忙?微調不對用戶作出反應

public class settings extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 
String selected; 
int themeno; 
String [] themes = {"Green with blue (Default)", "Green with red", "Green with orange", "Green with yellow", "Green with green", "Green with pink", "Green with purple"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("Settings", Context.MODE_PRIVATE); 

    themeno = sharedPref.getInt("Theme", 1); 

    Spinner themeSpinner = (Spinner) findViewById(R.id.spinner); 

    if(themeno == 1){ 
     setTheme(R.style.AppTheme); 
     themeSpinner.setSelection(1); 
    } else if (themeno == 2){ 
     setTheme(R.style.AppTheme2); 
     themeSpinner.setSelection(2); 
    } else if (themeno == 3){ 
     setTheme(R.style.AppTheme3); 
     themeSpinner.setSelection(3); 
    } else if (themeno == 4){ 
     setTheme(R.style.AppTheme4); 
     themeSpinner.setSelection(4); 
    } else if (themeno == 5){ 
     setTheme(R.style.AppTheme5); 
     themeSpinner.setSelection(5); 
    } else if (themeno == 6){ 
     setTheme(R.style.AppTheme6); 
     themeSpinner.setSelection(6); 
    } else if (themeno == 7){ 
     setTheme(R.style.AppTheme7); 
     themeSpinner.setSelection(7); 
    } 



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(settings.this, android.R.layout.simple_list_item_1 , themes); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    themeSpinner.setAdapter(adapter); 
} 


@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("Settings", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPref.edit(); 
    selected = parent.getItemAtPosition(position).toString(); 
    Toast toast = Toast.makeText(getApplicationContext(), "Theme selected. Colours will change when you close settings.", Toast.LENGTH_SHORT); 

    if(selected == "Green with blue (Default)"){ 
     editor.putInt("Theme", 1); 
     toast.show(); 
    } 

    if(selected == "Green with red"){ 
     editor.putInt("Theme", 2); 
     toast.show(); 
    } 

    if(selected == "Green with orange"){ 
     editor.putInt("Theme", 3); 
     toast.show(); 
    } 

    if(selected == "Green with yellow"){ 
     editor.putInt("Theme", 4); 
     toast.show(); 
    } 

    if(selected == "Green with green"){ 
     editor.putInt("Theme", 5); 
     toast.show(); 
    } 

    if(selected == "Green with pink"){ 
     editor.putInt("Theme", 6); 
     toast.show(); 
    } 

    if(selected == "Green with purple"){ 
     editor.putInt("Theme", 7); 
     toast.show(); 
    } 
} 

回答

2

首先,您需要將偵聽器添加到您的微調器。加入這行代碼裏面onCreate方法:

themeSpinner.setOnItemSelectedListener(this); 

接下來,==運營商正在恢復false所有if報表內。

您應該使用.equals()方法來比較字符串。 ==操作員檢查對象是否指向相同的內存位置。在你的情況下,selected變量和"any of strings in if statement"不是指相同的對象或內存。

另一方面,.equals()將比較變量的實際內容。因此,您通過if語句獲得了錯誤,並且沒有執行任何內部代碼。

if("Green with blue (Default)".equals(selected)) { 
    editor.putInt("Theme", 1); 
    toast.show(); 
} 
+0

它仍然無法正常工作。我添加了toast.show();在if語句之前查看它們是否是問題,但它甚至沒有顯示,所以我認爲該方法沒有被調用,但我不知道爲什麼。 –

+0

@zakwoolley不知道它是否會工作,但你可以嘗試更改'android.R.layout.simple_list_item_1'到'android.R.layout.simple_spinner_item'嗎?另外,如果'onItemSelected'被調用 – Marat

+0

否,那麼檢查'Log.v',這是行不通的。我檢查過,我不認爲onItemSelected被調用。 –