xuxiuxi
2017-03-30 8081d0d398ce1b2987f810e7e89e7c6fe473b4bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cn.com.basic.face.util;
 
import android.content.Context;
import android.content.res.AssetManager;
 
import cn.com.basic.face.model.CountryModel;
import cn.com.basic.face.model.CountrysModel;
 
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
/**
 * Created by Sinoe on 2017/2/28.
 */
 
public class CountryPresenter {
 
    protected String mCurrentCountrysWord;
    protected String mCurrentCountryName;
    protected String[] mCountryWordDatas;
    protected Map<String, String[]> mCountryDatasMap = new HashMap<>();
 
    private Context mContext;
 
    public CountryPresenter(Context mContext) {
        this.mContext = mContext;
        initCountrys();
    }
 
    private void initCountrys() {
        List<CountrysModel> countrysModelList = null;
        AssetManager asset = mContext.getAssets();
        try {
            InputStream input = asset.open("countrys.xml");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = spf.newSAXParser();
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();
            countrysModelList = handler.getDataList();
            if (countrysModelList != null && !countrysModelList.isEmpty()) {
                mCurrentCountrysWord = countrysModelList.get(0).getName();
                List<CountryModel> countryList = countrysModelList.get(0).getCountryModelList();
                if (countryList != null && !countryList.isEmpty()) {
                    mCurrentCountryName = countryList.get(0).getName();
                }
            }
            mCountryWordDatas = new String[countrysModelList.size()];
            for (int i = 0; i < countrysModelList.size(); i++) {
                mCountryWordDatas[i] = countrysModelList.get(i).getName();
                List<CountryModel> countryModelList = countrysModelList.get(i).getCountryModelList();
                String[] CountryNames = new String[countryModelList.size()];
                for (int j = 0; j < countryModelList.size(); j++) {
                    CountryNames[j] = countryModelList.get(j).getName();
                }
                mCountryDatasMap.put(countrysModelList.get(i).getName(), CountryNames);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
 
 
    public String[] getCountryWordData() {
        if (mCountryWordDatas == null) {
            initCountrys();
        }
        return mCountryWordDatas;
    }
    public HashMap<String, List<String>> getCountryData() {
        HashMap<String, List<String>> cityDataMap = new HashMap<>();
        if (mCountryDatasMap == null) {
            initCountrys();
        }
        for (int i = 0; i < mCountryDatasMap.size(); i++) {
            String[] cities = mCountryDatasMap.get(mCountryWordDatas[i]);
            if (cities == null) {
                cities = new String[]{""};
            }
            cityDataMap.put(mCountryWordDatas[i], Arrays.asList(cities));
        }
        return cityDataMap;
    }
}