編輯:關於Android編程
OrmLite可以幫助我們將會數據庫操作,不用自己去寫SQL語句,而且設置它跟使用Sqlite一樣的方式,它是一種關系型數據,我比較喜歡的就是它能夠幫助我判斷數據表項是更新還是創建等操作,不需要自己去寫大量的SQL語句去判斷,下面介紹一下它的使用文檔以及一些基本的注意點:
官方網址:http://ormlite.com/
JavaDOC地址:http://ormlite.com/javadoc/ormlite-android/
開發文檔地址:http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite.html
首先創建一個Model,作為一個數據表,在Android中我使用的是實現Parcelable,代碼如下:
package com.jwzhangjie.capricorntv.bean; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; import android.os.Parcel; import android.os.Parcelable; /** * * @author zj 包含視頻的參數 視頻的名字 "channel_id":9, "channel_name":"湖南衛視", * "icon_url":"http://tv.togic.com:8080/ShowTimeService/images/182.png", * "province":"湖南", "mode":"SD", "url": * "http://live.gslb.letv.com/gslb?stream_id=hunan&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1012&temporarykey=db7c39a0ee39ab2d4d2e781d5" * , * "second_url":["http://live-cdn.kksmg.com/channels/tvie/test/flv:500k" * , * "http://live.gslb.letv.com/gslb?stream_id=hunanHD_1800&tag=live&ext=m3u8&sign=live_tv&platid=10&splatid=1012&temporarykey=db7c39a0ee39ab2d4d2e781d5" * , * "http://pplive.shntv.cn/live/5/30/e9301e073cf94732a380b765c8b9573d.m3u8?type=ipad" * ,"rtsp://rlive.tv189.cn/live/112"], "types":"2|0" */ public class LiveItemBean implements Parcelable { @Override public int describeContents() { return 0; } public LiveItemBean() { } private LiveItemBean(Parcel source) { readFromParcel(source); } @DatabaseField(id = true) private int channel_id; @DatabaseField private String channel_name; @DatabaseField private String icon_url; @DatabaseField private String province; @DatabaseField private String mode; @DatabaseField private String url; @DatabaseField private String second_urls; private String[] second_url; @DatabaseField private String types; private void readFromParcel(Parcel source) { channel_name = source.readString(); icon_url = source.readString(); province = source.readString(); mode = source.readString(); url = source.readString(); second_urls = source.readString(); second_url = (String[]) source.readArray(LiveItemBean.class .getClassLoader()); types = source.readString(); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(channel_id); dest.writeString(channel_name); dest.writeString(icon_url); dest.writeString(province); dest.writeString(mode); dest.writeString(url); dest.writeString(second_urls); dest.writeArray(second_url); dest.writeString(types); } public static CreatorCREATOR = new Creator () { @Override public LiveItemBean createFromParcel(Parcel source) { return new LiveItemBean(source); } @Override public LiveItemBean[] newArray(int size) { return new LiveItemBean[size]; } }; public int getChannel_id() { return channel_id; } public void setChannel_id(int channel_id) { this.channel_id = channel_id; } public String getChannel_name() { return channel_name; } public void setChannel_name(String channel_name) { this.channel_name = channel_name; } public String getIcon_url() { return icon_url; } public void setIcon_url(String icon_url) { this.icon_url = icon_url; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getSecond_urls() { return second_urls; } public void setSecond_urls(String second_urls) { this.second_urls = second_urls; } public String[] getSecond_url() { return second_url; } public void setSecond_url(String[] second_url) { this.second_url = second_url; StringBuffer buffer = new StringBuffer(); int count = second_url.length; for (int i = 0; i < count; i++) { buffer.append(second_url[i]); if (i != count-1) { buffer.append(";"); } } second_urls = buffer.toString(); } public String getTypes() { return types; } public void setTypes(String types) { this.types = types; } }
1、是類名,這裡我使用的數據表的名字是默認類的小寫,當然你還可以指定表名字,使用@DatabaseTable(tableName = "liveitembeans"),
2、主鍵,在上面的代碼中有一行@DatabaseField(id = true)被注解對象就是主鍵,當然我們有時候使用的一個自增長的id,我們可以設置為@DatabaseField(generatedId = true)來實現,當然還有很多其他的注解配置我這裡就不一一說明了,自己可以到官網可以查看找到自己需要的。
package com.jwzhangjie.capricorntv.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; import com.jwzhangjie.capricorntv.bean.LiveItemBean; public class DBHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = "jwzhangjie.db"; private static final int DATABASE_VERSION = 1; public DBHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, LiveItemBean.class); } catch (Exception e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) { try { TableUtils.dropTable(connectionSource, LiveItemBean.class, true); onCreate(sqLiteDatabase, connectionSource); } catch (Exception e) { e.printStackTrace(); } } }上面使用TableUtils來創建和刪除表,還有其他的功能例如清空表內容等
接下來就是創建一個操作數據庫的工具DAO
package com.jwzhangjie.capricorntv.uitls; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import android.content.Context; import com.j256.ormlite.dao.Dao; import com.jwzhangjie.capricorntv.bean.LiveItemBean; import com.jwzhangjie.capricorntv.db.DBHelper; public class DBUtils { public static DaoliveDao = null; public DBUtils(Context context) { if (liveDao == null) { DBHelper dbHelper = new DBHelper(context); try { liveDao = dbHelper.getDao(LiveItemBean.class); } catch (SQLException e) { e.printStackTrace(); } } } /** * 插入直播數據,如果數據存在則進行更新 * * @param liveItemBean */ public void LiveCreate(LiveItemBean liveItemBean) { try { liveDao.createOrUpdate(liveItemBean); } catch (SQLException e) { e.printStackTrace(); } } /** * 連續進行插入,如果存在則更新 */ public void LiveCreates(List lists) { try { for (LiveItemBean liveItemBean : lists) { liveDao.createOrUpdate(liveItemBean); } } catch (Exception e) { e.printStackTrace(); } } /** * 查詢所有的直播元素 * @return */ public List getLiveItemBeans(){ List listsBeans = new ArrayList (); try { listsBeans = liveDao.queryForAll(); } catch (SQLException e) { e.printStackTrace(); } return listsBeans; } }
/** * 連續進行插入,如果存在則更新 */ public void LiveCreates(final List查詢除了用已有的接口,我們還可以使用sql語句來實現,比如:lists) { try { liveDao.callBatchTasks(new Callable () { @Override public Void call() throws Exception { for (LiveItemBean liveItemBean : lists) { liveDao.createOrUpdate(liveItemBean); } return null; } }); } catch (Exception e) { e.printStackTrace(); } }
// find out how many orders account-id #10 has GenericRawResultsrawResults = orderDao.queryRaw( "select count(*) from orders where account_id = 10"); // there should be 1 result List results = rawResults.getResults(); // the results array should have 1 value String[] resultArray = results.get(0); // this should print the number of orders that have this account-id System.out.println("Account-id 10 has " + resultArray[0] + " orders");
當我們應用退出時候,我們需要釋放之前對象
OpenHelperManager.releaseHelper(); dbHelper = null;
現在開發Android程序基本都已經從Eclipse轉到了Android Studio了, 最近項目需求, 需要用到ndk編程, 於是就折騰了一下。開發環境Android
RecyclerView出來很長時間了,相信大家都已經比較了解了,這裡我把知識梳理一下,其實你把他看成一個升級版的ListView也是可以的,為什麼這樣說呢?我們一起來
一、Android平台濾鏡濾鏡這個功能在目前的市場上應用很廣泛,發展也非常快,總結起來,基本上有以下三種應用會包含濾鏡功能,都各有所長。 二、相機濾鏡介紹1、相
這篇博客稍微講解下React-Native中的布局。比較簡單。RN的而布局是用css中的flexbox布局,所以布局起來與android傳統的布局樣式有點像。接下來就結合