我正在一个项目中工作,我必须在列表视图中显示系统可用的语言环境,格式如下:
所以我在 onCreate 中完成了这个:
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(getContentView());
String[] locales = getAssets().getLocales(); // all system locale
Arrays.sort(locales); // sort in lexicographic order
final int origSize = locales.length;
// Loc is a class that I've expalined later in this question
Loc[] preprocess = new Loc[origSize];
int finalSize = 0;
for (int i = 0; i < origSize; i++) {
String s = locales[i];
int len = s.length(); // i.e. en_US
if (len == 5) {
String language = s.substring(0, 2); // i.e. en
String country = s.substring(3, 5); // i.e. US
Locale l = new Locale(language, country);
// There are some other logics. I excluded those for simplicity
// and to focus the main problem
preprocess[finalSize++] = new Loc(
toTitleCase(l.getDisplayName(l)), l);
}
}
mLocales = new Loc[finalSize + 1];
// put into another array keeping it's first index empty
for (int i = 0; i < finalSize; i++) {
mLocales[i + 1] = preprocess[i];
}
// put the system default to show it at the first index
mLocales[0] = new Loc("Use System Default", Resources
.getSystem().getConfiguration().locale);
// pass the array to Listview
int layoutId = R.layout.locale_picker_item;
int fieldId = R.id.locale;
ArrayAdapter<Loc> adapter = new ArrayAdapter<Loc>(this, layoutId,
fieldId, mLocales);
getListView().setAdapter(adapter);
}
Loc类是:
public static class Loc {
String label;
Locale locale;
public Loc(String label, Locale locale) {
this.label = label;
this.locale = locale;
}
@Override
public String toString() {
// for the first index, it should show system default
if (this.label.equals("Use System Default")
return (this.label + " (" + this.locale.getDisplayName() + ", "
+ this.locale.getCountry() + ")");
return this.locale.getDisplayName(this.locale);
}
}
预期行为:
________________________________
Use System Default (English, US)
________________________________
বাংলা (বাংলাদেশ)
________________________________
বাংলা (ভারত)
________________________________
English (United States)
....
....
....
但就我而言,
________________________________
English (United States)
________________________________
বাংলা (বাংলাদেশ)
________________________________
বাংলা (ভারত)
________________________________
English (United States)
....
....
....
所以我的问题是,为什么我想在第一个索引的列表视图中显示的文本没有显示?