Android Open Source - firstcodeandroid Cool Weather D B






From Project

Back to project page firstcodeandroid.

License

The source code is released under:

MIT License

If you think the Android project firstcodeandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vjia.coolweather.db;
//from w w  w .  j  av  a  2 s .  co m
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.vjia.coolweather.db.CoolWeatherOpenHelper;
import com.vjia.coolweather.model.City;
import com.vjia.coolweather.model.County;
import com.vjia.coolweather.model.Province;

public class CoolWeatherDB {
  private static final String tag = CoolWeatherDB.class.getName();
  public static final String DB_NAME = "cool_weather";
  public static final int VERSION = 1;
  private static CoolWeatherDB coolWeatherDB;
  private SQLiteDatabase db;
  
  /* for now When click CHINA , in LogCat will see java.lang.OutOfMemoryError:[memory exhausted],
   * so to make a province/city/county size limit, let's set 20 for now.
   */
  @Deprecated
  private int limitSize = 20;

  private CoolWeatherDB(Context context) {
    CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context,
        DB_NAME, null, VERSION);
    db = dbHelper.getWritableDatabase();
  }

  public synchronized static CoolWeatherDB getInstance(Context context) {
    if (coolWeatherDB == null) {
      coolWeatherDB = new CoolWeatherDB(context);
    }
    return coolWeatherDB;
  }

  public void saveProvince(Province province) {
    if (province != null) {
      ContentValues values = new ContentValues();
      values.put("province_name", province.getProvinceName());
      values.put("province_code", province.getProvinceCode());
      db.insert("Province", null, values);
      Log.d(tag, "a Province is inserted.");
    }
  }

  public List<Province> loadProvinces() {
    List<Province> list = new ArrayList<Province>();
    Cursor cursor = db
        .query("Province", null, null, null, null, null, null);
    int i=0;
    if (cursor.moveToFirst()) {
      do {
        Province province = new Province();
        province.setId(cursor.getInt(cursor.getColumnIndex("id")));
        province.setProvinceName(cursor.getString(cursor
            .getColumnIndex("province_name")));
        province.setProvinceCode(cursor.getString(cursor
            .getColumnIndex("province_code")));
        list.add(province);
        if((i++) > 20) break;
      } while (cursor.moveToNext());
    }
    Log.d(tag, "loadProvinces() done.");
    return list;
  }

  public void saveCity(City city) {
    if (city != null) {
      ContentValues values = new ContentValues();
      values.put("city_name", city.getCityName());
      values.put("city_code", city.getCityCode());
      values.put("province_id", city.getProvinceId());
      db.insert("City", null, values);
      Log.d(tag, "a City is inserted.");
    }
  }

  public List<City> loadCities(int provinceId) {
    List<City> list = new ArrayList<City>();
    Cursor cursor = db.query("City", null, "province_id=?",
        new String[] { String.valueOf(provinceId) }, null, null, null);
    int i=0;
    if (cursor.moveToFirst()) {
      do {
        City city = new City();
        city.setId(cursor.getInt(cursor.getColumnIndex("id")));
        city.setCityName(cursor.getString(cursor
            .getColumnIndex("city_name")));
        city.setCityCode(cursor.getString(cursor
            .getColumnIndex("city_code")));
        city.setProvinceId(provinceId);
        list.add(city);
        if((i++) > 20) break;
      } while (cursor.moveToFirst());
    }
    return list;
  }
  
  public void saveCounty(County county){
    if(county!=null){
      ContentValues values=new ContentValues();
      values.put("county_name", county.getCountyName());
      values.put("county_code", county.getCountyCode());
      values.put("city_id", county.getCityId());
      db.insert("County", null, values);
      Log.d(tag, "a County is inserted.");
    }
  }
  
  public List<County> loadCounties(int cityId) {
    List<County> list = new ArrayList<County>();
    Cursor cursor = db.query("County", null, "city_id=?",
        new String[] { String.valueOf(cityId) }, null, null, null);
    int i=0;
    if (cursor.moveToFirst()) {
      do {
        County county = new County();
        county.setId(cursor.getInt(cursor.getColumnIndex("id")));
        county.setCountyName(cursor.getString(cursor
            .getColumnIndex("county_name")));
        county.setCountyCode(cursor.getString(cursor
            .getColumnIndex("county_code")));
        county.setCityId(cityId);
        list.add(county);
        if((i++) > 20) break;
      } while (cursor.moveToFirst());
    }
    return list;
  }

}




Java Source Code List

com.example.activitylifecycletest.DialogActivity.java
com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.NormalActivity.java
com.example.activitytest.FirstActivity.java
com.example.listviewtest.FruitAdapter.java
com.example.listviewtest.Fruit.java
com.example.listviewtest.MainActivity.java
com.jikexueyuan.counttime.MainActivity.java
com.jikexueyuan.getmyphonenumber.GetNumber.java
com.jikexueyuan.getmyphonenumber.MainActivity.java
com.jikexueyuan.getmyphonenumber.MyAdapter.java
com.jikexueyuan.getmyphonenumber.PhoneInfo.java
com.vjia.bookcollector.MainActivity.java
com.vjia.coolweather.MainActivity.java
com.vjia.coolweather.activity.ChooseAreaActivity.java
com.vjia.coolweather.activity.WeatherActivity.java
com.vjia.coolweather.db.CoolWeatherDB.java
com.vjia.coolweather.db.CoolWeatherOpenHelper.java
com.vjia.coolweather.model.City.java
com.vjia.coolweather.model.County.java
com.vjia.coolweather.model.Province.java
com.vjia.coolweather.util.HttpCallbackListener.java
com.vjia.coolweather.util.HttpUtil.java
com.vjia.coolweather.util.Utility.java
com.vjia.helloandroid.FirstActivity.java
com.vjia.helloandroid.HelloAndroidActivity.java
com.vjia.hellonote.AddContent.java
com.vjia.hellonote.MainActivity.java
com.vjia.hellonote.MyAdapter.java
com.vjia.hellonote.NotesDB.java
com.vjia.hellonote.SelectAct.java
com.vjia.jokeking.GetJoke.java
com.vjia.jokeking.HttpCallbackListener.java
com.vjia.jokeking.HttpUtil.java
com.vjia.jokeking.Joke.java
com.vjia.jokeking.MainActivity.java
com.vjia.jokeking.MyAdapter.java
com.vjia.locationtest.MainActivity.java