百度定位并获取县区天气-XML+fragment+sqlite

此工程较BaiduLocationXMLFragment相比:
1.加入数据库部分,将获取到的地址  天气存入数据库中,离线状态显示数据库最后一条记录

sqlite:

DatabaseHelper.java

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
     
    private static final String DB_NAME = "weatherDB.db"; 
    private static final int version = 1; 
    public static final String TABLE_NAME = "weatherInfo";
     
    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, version);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        
        db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"(recordID INTEGER PRIMARY KEY,address VARCHAR,weather VARCHAR)");  
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
 
    }

}

DBManger.java

//http://blog.csdn.net/liuhe688/article/details/6715983
public class DBManager
{
    private DatabaseHelper helper;
    private SQLiteDatabase db;

    public DBManager(Context context)
    {
        Log.d("sqlite", "DBManager --> Constructor");
        helper = new DatabaseHelper(context);
        db = helper.getWritableDatabase();
    }

    /**
     * add weatherInfo
     * 
     * @param wibs  也可以设计成WeatherInfoBean对象
     * 
     */
    public void add(List<WeatherInfoBean> wibs)
    {
        Log.d("sqlite", "DBManager --> add");
        db.beginTransaction();
        try
        {
            for (WeatherInfoBean wib: wibs)
            {
                db.execSQL("INSERT INTO " + DatabaseHelper.TABLE_NAME
                        + " VALUES(?,?,?)", new Object[] { null,wib.getAddr(),
                        wib.getWeather() });
            }
            db.setTransactionSuccessful();
        }
        finally
        {
            db.endTransaction(); 
        }
    }


    /**
     * query all weatherInfo, return list
     * 
     * @return List<Person>
     */
    public List<WeatherInfoBean> query()
    {
        Log.d("sqlite", "DBManager --> query");
        ArrayList<WeatherInfoBean> wibs = new ArrayList<WeatherInfoBean>();
        Cursor c = queryTheCursor();
        while (c.moveToNext())
        {
            WeatherInfoBean wib = new WeatherInfoBean();
            wib.setAddr(c.getString(c.getColumnIndex("address")));
            wib.setWeather(c.getString(c.getColumnIndex("weather")));
            wibs.add(wib);
        }
        c.close();
        return wibs;
    }

    /**
     * query all persons, return cursor
     * 
     * @return Cursor
     */
   public Cursor queryTheCursor()
    {
        Log.d("sqlite", "DBManager --> queryTheCursor");
        Cursor c = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME,
                null);
        Log.i("sqlite","xxxxxxxxx");
        return c;
        
    }
//查询最后一条记录  返回对象
   public WeatherInfoBean queryCertainItem(){
       WeatherInfoBean wib=new WeatherInfoBean();
       db.beginTransaction();
       try
       {
           
           Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME,
                   null);
           if (cursor.moveToLast()) {
               // 该cursor是最后一条数据
    
               wib.setAddr(cursor.getString(cursor.getColumnIndex("address")));
               wib.setWeather(cursor.getString(cursor.getColumnIndex("weather")));
           }
       }
       finally
       {
           db.endTransaction(); 
       }
    return wib;  
   }
    /**
     * close database
     */
    public void closeDB()
    {
        Log.d("sqlite", "DBManager --> closeDB");
        db.close();
    }

}
源码:http://download.csdn.net/detail/xiejun1026/8444571

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。