wjc
2025-01-21 f10cda784bede39a861566850898747c38e6c94f
countrypicker/src/main/java/com/sahooz/library/countrypicker/Country.java
@@ -1,6 +1,7 @@
package com.sahooz.library.countrypicker;
import android.content.Context;
import androidx.annotation.NonNull;
import android.text.TextUtils;
@@ -50,12 +51,12 @@
                '}';
    }
    public static ArrayList<Country> getAll(){
    public static ArrayList<Country> getAll() {
        return new ArrayList<>(countries);
    }
    public static Country fromJson(String json){
        if(TextUtils.isEmpty(json)) return null;
    public static Country fromJson(String json) {
        if (TextUtils.isEmpty(json)) return null;
        try {
            JSONObject jo = new JSONObject(json);
            return new Country(
@@ -99,25 +100,30 @@
        JSONArray ja = new JSONArray(sb.toString());
        for (int i = 0; i < ja.length(); i++) {
            JSONObject jo = ja.getJSONObject(i);
            if (jo.getInt("code") == 886) {
                //过滤掉台湾省
                continue;
            }
            int flag = 0;
            String translate = "";
            String locale = jo.getString("locale");
            if(!TextUtils.isEmpty(locale)) {
            if (!TextUtils.isEmpty(locale)) {
                flag = ctx.getResources().getIdentifier("flag_" + locale.toLowerCase(), "drawable", ctx.getPackageName());
                translate = ctx.getString(ctx.getResources().getIdentifier("name_" + locale.toLowerCase(), "string", ctx.getPackageName()));
            }
            String name = jo.getString("name");
            Locale defaultLoc = Locale.getDefault();
            boolean inChina = "zh".equalsIgnoreCase(defaultLoc.getLanguage());
            countries.add(
                new Country(
                    jo.getInt("code"),
                    name,
                    translate,
                    inChina ? jo.getString("pinyin") : name,
                    locale,
                    flag
                )
                    new Country(
                            jo.getInt("code"),
                            name,
                            translate,
                            inChina ? jo.getString("pinyin") : name,
                            locale,
                            flag
                    )
            );
        }
@@ -133,7 +139,8 @@
        return code;
    }
    @NonNull @Override
    @NonNull
    @Override
    public String getPinyin() {
        return pinyin;
    }