示例#1
0
  public void queryCountry() {
    country = weatherDB.loadCountry(choiceCity);

    if (country.size() > 0) {
      dataList.clear();
      for (String p : country) {
        dataList.add(p);
      }
      adapter.notifyDataSetChanged();
      lvChoice.setSelection(0);
      tvTitle.setText(choiceCity);
      currentLevel = LEVEL_COUNTRY;
    }
  }
示例#2
0
  public void queryCity() {
    city = weatherDB.loadCity(choiceProvince);

    if (city.size() > 0) {
      dataList.clear();
      for (String p : city) {
        dataList.add(p);
      }
      adapter.notifyDataSetChanged();
      lvChoice.setSelection(0);
      tvTitle.setText(choiceProvince);
      currentLevel = LEVEL_CITY;
    }
  }
示例#3
0
  public void loadData() {

    int num = weatherDB.db.query("City_Info", null, null, null, null, null, null).getCount();
    // 往数据库写入初始城市信息 写入时间太长,建议再开个线程
    if (num == 0) {

      InputStream inputStream = getResources().openRawResource(R.raw.city);
      InputStreamReader inputStreamReader = null;
      try {

        inputStreamReader = new InputStreamReader(inputStream, "gbk");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }

      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String line;
      try {

        while ((line = bufferedReader.readLine()) != null) {

          if (line.length() > 1) {
            try {
              JSONObject jsonObject = new JSONObject(line);
              CityInfo cityInfo = new CityInfo();

              cityInfo.setNationName(jsonObject.getString("parent3"));
              cityInfo.setProviceName(jsonObject.getString("parent2"));
              cityInfo.setCityName(jsonObject.getString("parent1"));
              cityInfo.setCountryName(jsonObject.getString("name"));
              cityInfo.setCountryPingyin(jsonObject.getString("en"));
              cityInfo.setCityId(jsonObject.getString("id"));
              weatherDB.saveCityInfo(cityInfo);
              //                            String id = jsonObject.getString("id");
              //                            String country_name = jsonObject.getString("name");
              //                            String city_name = jsonObject.getString("parent1");
              //                            String province_name = jsonObject.getString("parent2");
              //                            String nation_name = jsonObject.getString("parent3");
              //                            String country_pinying = jsonObject.getString("en");

            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
示例#4
0
  public void queryProvince() {
    province = weatherDB.loadProvince();

    if (province.size() > 0) {
      dataList.clear();
      for (String p : province) {
        dataList.add(p);
      }
      adapter.notifyDataSetChanged();
      lvChoice.setSelection(0);
      tvTitle.setText("中国");
      currentLevel = LEVEL_PROVINCE;
    }
  }
示例#5
0
  public void init() {
    btnData = (Button) findViewById(R.id.btnData);
    ivWeather = (ImageView) findViewById(R.id.ivWeather);
    tvTitle = (TextView) findViewById(R.id.tvTitle);
    lvChoice = (ListView) findViewById(R.id.lvChoice);

    // = new CoolWeatherOpenHelper(this,"CityInfo.db",null,1);
    weatherDB = WeatherDB.getInstance(this);
    loadData();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);
    lvChoice.setAdapter(adapter);
    lvChoice.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (currentLevel == LEVEL_PROVINCE) {
              choiceProvince = (String) province.toArray()[position];
              queryCity();
            } else if (currentLevel == LEVEL_CITY) {
              choiceCity = (String) city.toArray()[position];
              queryCountry();
            }
          }
        });

    queryProvince();

    //        int num = weatherDB.db.query("City_Info", null, null, null, null, null,
    // null).getCount();
    //        //往数据库写入初始城市信息 写入时间太长,建议再开个线程
    //        if (num == 0) {
    //
    //            InputStream inputStream = getResources().openRawResource(R.raw.city);
    //            InputStreamReader inputStreamReader = null;
    //            try {
    //
    //                inputStreamReader = new InputStreamReader(inputStream, "gbk");
    //            } catch (UnsupportedEncodingException e) {
    //                e.printStackTrace();
    //            }

    //            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    //            String line;
    //            try {
    //                while ((line = bufferedReader.readLine()) != null) {
    //
    //                    if (line.length() > 1) {
    //                        try {
    //                            JSONObject jsonObject = new JSONObject(line);
    //                            CityInfo cityInfo = new CityInfo();
    //
    //                            cityInfo.setNationName(jsonObject.getString("parent3"));
    //                            cityInfo.setProviceName(jsonObject.getString("parent2"));
    //                            cityInfo.setCityName(jsonObject.getString("parent1"));
    //                            cityInfo.setCountryName(jsonObject.getString("name"));
    //                            cityInfo.setCountryPingyin(jsonObject.getString("en"));
    //                            cityInfo.setCityId(jsonObject.getString("id"));
    //                            weatherDB.saveCityInfo(cityInfo);
    //                            String id = jsonObject.getString("id");
    //                            String country_name = jsonObject.getString("name");
    //                            String city_name = jsonObject.getString("parent1");
    //                            String province_name = jsonObject.getString("parent2");
    //                            String nation_name = jsonObject.getString("parent3");
    //                            String country_pinying = jsonObject.getString("en");
    //
    //                        } catch (Exception e) {
    //                            e.printStackTrace();
    //                        }
    //                    }
    //                }
    //            } catch (IOException e) {
    //                e.printStackTrace();
    //            }
    //        }

    btnData.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // dbHelper.getWritableDatabase();

            ivWeather.setImageResource(R.drawable.a13);

            int n = weatherDB.db.query("City_Info", null, null, null, null, null, null).getCount();
            Log.d("aaaaa", "----->" + n);

            LinkedHashSet<String> cityName = weatherDB.loadCountry("宜昌");

            Log.e("aaaaa", "----->" + cityName.size());
            Iterator iterator = cityName.iterator();
            while (iterator.hasNext()) {
              Log.e("aaaaa", iterator.next().toString());
            }
          }
        });
  }