覆盖Android ListView的toString()会产生意外结果

时间:2013-05-23 14:02:24

标签: android listview locale

我正在一个项目中,我必须使用以下格式在listview 中显示系统的可用语言环境:

所以我在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)
     ....
     ....
     ....

所以我的问题是,为什么我要在第一个索引的listview中显示的文字,而不显示?

2 个答案:

答案 0 :(得分:0)

你要比较的字符串中有一个拼写错误:

  

mLocales[0] = new Loc("Use System Default" ...

  

if (this.label.equals("Use Sytem Default") ...

答案 1 :(得分:0)

系统拼写中的问题。你正在检查Sytem

DO

this.label.equals("Use System Default")

而不是

this.label.equals("Use Sytem Default")