Android Open Source - android-bossweather City D A O






From Project

Back to project page android-bossweather.

License

The source code is released under:

Apache License

If you think the Android project android-bossweather 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

/*
 * Copyright (C) 2013 The Android Open Source Project
 *//ww  w  .j  a v  a  2  s . c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.michael.feng.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.michael.feng.model.City;

public class CityDAO {
  // Database fields
  private SQLiteDatabase database;
  private SQLiteHelper dbHelper;
  private String[] allColumns = { 
      SQLiteHelper.COL_ID, 
      SQLiteHelper.COL_NAME,
      SQLiteHelper.COL_PROVINCE,
      SQLiteHelper.COL_CODE,
      SQLiteHelper.COL_STATUS};

  public CityDAO(Context context) {
    dbHelper = new SQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public City insertCity(City city) {
    ContentValues values = new ContentValues();
    values.put(SQLiteHelper.COL_NAME,     city.getName());
    values.put(SQLiteHelper.COL_PROVINCE, city.getProvince());
    values.put(SQLiteHelper.COL_CODE,     city.getCode());
    values.put(SQLiteHelper.COL_STATUS,   city.getStatus());
    values.put(SQLiteHelper.COL_UPDATED,   city.getUpdated().toString());
    long insertId = database.insert(SQLiteHelper.TB_NAME, null, values);
    Cursor cursor = database.query(SQLiteHelper.TB_NAME, allColumns, SQLiteHelper.COL_ID
        + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    City newCity = cursorToCity(cursor);
    cursor.close();
    return newCity;
  }
  
  public List<City> queryCityByCode(String code) {
    List<City> cityList = new ArrayList<City>();
    String querySql = "SELECT _id, name, province, code, status, updated FROM cities WHERE code LIKE '%" + code + "%' ORDER BY updated ASC";
    Cursor cursor = database.rawQuery(querySql, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      City city = cursorToCity(cursor);
      cityList.add(city);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return cityList;
  }
  
  public List<City> queryCityByName(String name) {
    List<City> cityList = new ArrayList<City>();
    String querySql = "SELECT _id, name, province, code, status, updated FROM cities WHERE name LIKE '%" + name + "%' ORDER BY updated ASC";
    Cursor cursor = database.rawQuery(querySql, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      City city = cursorToCity(cursor);
      cityList.add(city);
      cursor.moveToNext();
    }
    cursor.close();
    return cityList;
  }
  
  public List<City> getMyCities() {
    List<City> cityList = new ArrayList<City>();
    String querySql = "SELECT _id, name, province, code, status, updated FROM cities WHERE status = 1 ORDER BY updated ASC";
    Cursor cursor = database.rawQuery(querySql, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      City city = cursorToCity(cursor);
      cityList.add(city);
      cursor.moveToNext();
    }
    cursor.close();
    return cityList;
  }
  
  public void updateCityStatus(City city, String status) {
    ContentValues cv = new ContentValues();
    cv.put("status", status);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date = sdf.format(new Date());
    cv.put("updated", date);
    database.update(SQLiteHelper.TB_NAME, cv, 
        SQLiteHelper.COL_NAME     + " = '" + city.getName() +     "' and "+ 
        SQLiteHelper.COL_PROVINCE + " = '" + city.getProvince() + "'", null);
  }

  private City cursorToCity(Cursor cursor) {
    City city = new City();
    city.setId(cursor.getInt(0));
    city.setName(cursor.getString(1));
    city.setProvince(cursor.getString(2));
    city.setCode(cursor.getString(3));
    city.setStatus(cursor.getString(4));
    
    // Date format in DB convert into Java Calendar
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date cal = null;
      try {
        cal= sdf.parse(cursor.getString(5));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    city.setUpdated(cal);
    return city;
  }
  
}




Java Source Code List

com.michael.feng.bossweather.AddActivity.java
com.michael.feng.bossweather.EditActivity.java
com.michael.feng.bossweather.MainActivity.java
com.michael.feng.model.City.java
com.michael.feng.tools.CityDAO.java
com.michael.feng.tools.ConvertUtil.java
com.michael.feng.tools.ImageUtils.java
com.michael.feng.tools.MyCard.java
com.michael.feng.tools.MyImageCard.java
com.michael.feng.tools.SQLiteHelper.java
com.michael.feng.utils.YahooWeather4a.ConditionDefinition.java
com.michael.feng.utils.YahooWeather4a.WOEIDUtils.java
com.michael.feng.utils.YahooWeather4a.WeatherInfo.java
com.michael.feng.utils.YahooWeather4a.YahooWeatherInfoListener.java
com.michael.feng.utils.YahooWeather4a.YahooWeatherUtils.java