我想作一個簡單的本地化的演示,我有一個微調,選擇語言和文本視圖來顯示所需語言的字符串,這裏是我的代碼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);
}
現在,它遞歸觸發,我不能點擊微調和膨脹其項目!
我添加了一個條件來停止遞歸,我會後我的代碼在任何人的情況下,得到了porblem太: ) –