我已经实现了应用程序,其中我点击列表项后我有更改语言并且它被更改但在杀死应用程序后语言状态未保存.....虽然我已将其保存到共享首选项....
这是我的代码:
public class Languages extends Fragment {
// private int current = -1;
View view;
Context ct;
SharedPreferences sp1, sp;
Boolean Ischeck = false;
String User_id, countryid;
Button settings;
// String[] arrLanguages = new String[]{"English", "Arabic"};
String[] arrLanguages = new String[] { "English", "Arabic" };
String req, locale;
ListView lv;
private Locale myLocale;
public Languages(Context c) {
// TODO Auto-generated constructor stub
this.ct = c;
}
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.languagechange, container, false);
SharedPreferences sp = getActivity().getSharedPreferences("key", 0);
User_id = sp.getString("userid=", User_id);
countryid = sp.getString("countryid", countryid);
settings = (Button) view.findViewById(R.id.btnsett);
lv = (ListView) view.findViewById(android.R.id.list);
lv.setAdapter(new MobileArrayAdapter(ct, arrLanguages));
settings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
FragmentManager manager = getFragmentManager();
manager.popBackStack();
Log.d("Back to Home", "back button pressed");
}
});
loadLocale();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// get selected items
String selectedValue = (String) lv.getAdapter().getItem(
position);
Toast.makeText(ct, selectedValue, Toast.LENGTH_SHORT).show();
// setResult(RESULT_OK, new Intent().putExtra("language",
// arrLanguages[position]));
// Configuration config = new Configuration();
String lang = "en";
switch (position) {
case 0:
lang = "en";
break;
case 1:
lang = "ar";
break;
default:
break;
}
changeLang(lang);
// getResources().updateConfiguration(config, null);
lv.setItemChecked(position, true);
}
});
return view;
}
public void loadLocale() {
String langPref = "Language";
SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
public void saveLocale(String lang) {
String langPref = "Language";
SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
public void changeLang(String lang) {
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
ct.getResources().updateConfiguration(config,
ct.getResources().getDisplayMetrics());
}
class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_changelanguage, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_changelanguage,
parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView
.findViewById(R.id.imageView1);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (lv.isItemChecked(position)) {
imageView.setVisibility(View.VISIBLE);
Ischeck = true;
SharedPreferences sp = getActivity().getSharedPreferences(
"check", 0);
SharedPreferences.Editor sedt = sp.edit();
sedt.putBoolean("Ischeck", Ischeck = true);
sedt.clear();
sedt.commit();
} else {
imageView.setVisibility(View.GONE);
}
return rowView;
}
}
@Override
public void onConfigurationChanged(
android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (myLocale != null) {
newConfig.locale = myLocale;
Locale.setDefault(myLocale);
ct.getResources().updateConfiguration(newConfig,
ct.getResources().getDisplayMetrics());
}
}
答案 0 :(得分:0)
尝试改变。
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// get selected items
String selectedValue = (String) lv.getAdapter().getItem(
position);
Toast.makeText(ct, selectedValue, Toast.LENGTH_SHORT).show();
// setResult(RESULT_OK, new Intent().putExtra("language",
// arrLanguages[position]));
// Configuration config = new Configuration();
String lang = "" + selectedValue;
changeLang(lang);
// getResources().updateConfiguration(config, null);
lv.setItemChecked(position, true);
}
});
return view;
}