2013-10-06 50 views
0

我想作一個簡單的本地化的演示,我有一個微調,選擇語言和文本視圖來顯示所需語言的字符串,這裏是我的代碼Android的微調項目不產生,同時改變本地化

public class MainActivity extends Activity implements OnItemSelectedListener 
{ 
    Spinner laguageSpinner; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     laguageSpinner = laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 
     laguageSpinner.setOnItemSelectedListener(this); 
     generateLanguageItems(); 
    } 
    public void generateLanguageItems() 
    { 
     laguageSpinner = laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 
     List<String> list = new ArrayList<String>(); 
     list.add("English"); 
     list.add("عربي"); 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       R.layout.el_spinner_txtview, list); 
     dataAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     laguageSpinner.setAdapter(dataAdapter); 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> adapter, View arg1, int pos, 
      long arg3) 
    { 
     Toast.makeText(this, adapter.getItemAtPosition(pos) + "", 
       2000).show(); 
     switch(pos) 
     {case 0: changeLocale("en");break; 
     case 1:  changeLocale("ar");break;} 
    } 

    public void changeLocale(String code) 
    { 
     Locale locale = new Locale(code); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     getBaseContext().getResources().updateConfiguration(config, 
     getBaseContext().getResources().getDisplayMetrics()); 
     this.setContentView(R.layout.activity_main); 
     generateLanguageItems(); 
    } 
} 

本地化改變代碼工作完美,我已經改變測試過這個

switch(pos) 
    {case 0: changeLocale("en");break; 
    case 1:  changeLocale("ar");break;} 

switch(pos) 
    {case 0: changeLocale("ar");break; 
    case 1:  changeLocale("en");break;} 

我的問題在這裏,微調項目不會再生成運行我有一個空的微調,試圖做一些代碼修改,但沒有運氣,你能幫我嗎?

---編輯--- 作爲事實上我試着這樣做

public void generateLanguageItems() 
{ 
    .... 
    laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 
    laguageSpinner.setAdapter(dataAdapter); 
} 

這樣微調的項目不產生但onItemSelected()不會觸發了,然後 我添加

public void generateLanguageItems() 
{ 
      ... 
    laguageSpinner.setOnItemSelectedListener(null); 
    laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 
    laguageSpinner.setAdapter(dataAdapter); 
    laguageSpinner.setOnItemSelectedListener(this); 
} 

現在,它遞歸觸發,我不能點擊微調和膨脹其項目!

+0

我添加了一個條件來停止遞歸,我會後我的代碼在任何人的情況下,得到了porblem太: ) –

回答

0

我添加了一個條件,打破遞歸,這裏是工作的代碼

public class MainActivity extends Activity implements OnItemSelectedListener 
{ 
    Spinner laguageSpinner; 
    int currentLocale=0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 
     laguageSpinner.setOnItemSelectedListener(this); 
     generateLanguageItems(0); 
    } 
    public void generateLanguageItems(int pos) 
    { 
     List<String> list = new ArrayList<String>(); 
     list.add("English"); 
     list.add("عربي"); 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       R.layout.el_spinner_txtview, list); 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     laguageSpinner.setOnItemSelectedListener(null); 
     laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 
     laguageSpinner.setAdapter(dataAdapter); 
     laguageSpinner.setOnItemSelectedListener(this); 
     laguageSpinner.setSelection(pos); 
     currentLocale = pos; 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> adapter, View arg1, int pos, long arg3) 
    { changeLocale( pos); } 

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

    public void changeLocale(int pos) 
    { 
     if (currentLocale == pos) 
      return; 
     Locale locale; 
     if (pos == 0) 
      locale = new Locale("en"); 
     else 
      locale = new Locale("ar"); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     getBaseContext().getResources().updateConfiguration(config, 
       getBaseContext().getResources().getDisplayMetrics()); 
     this.setContentView(R.layout.activity_main); 
     generateLanguageItems(pos); 
    } 
} 
0

爲什麼你有這樣的雙重任務:

laguageSpinner = laguageSpinner = ((Spinner) findViewById(R.id.spinner1)); 

改變,要

laguageSpinner = (Spinner) findViewById(R.id.spinner1); 

,然後再試一次。

+0

沒有! does not work –

+0

你有2個位子需要一個。你在兩個方面都改變了嗎? – Szymon

+0

我解決了我將解決方案作爲編輯發佈的問題 –

相關問題