百度定位並擷取縣區天氣-XML+fragment+sqlite

來源:互聯網
上載者:User

標籤:

此工程較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/6715983public 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

百度定位並擷取縣區天氣-XML+fragment+sqlite

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.