Android ORM應用開發框架KJFrameForAndroid使用詳解
本文將為大家介紹一款Android ORM應用開發框架KJFrameForAndroid,很多時候我們也叫它KJLibrary。
KJFrameForAndroid簡介
KJFrameForAndroid是一款基于Android的ORM和 IOC應用開發框架,封裝了很多Android開發中常用的功能,包括Android中對Bitmap的操作類庫。KJFrameForAndroid的設計非常精簡,利用KJFrameForAndroid,我們可以用最少的代碼完成很多豐富的Android功能應用,為Android開發者節省許多不必要的開發時間。
KJFrameForAndroid總共分為五大模塊:UILibrary,UtilsLibrary,HttpLibrary,BitmapLibrary,DBLibrary。
KJFrameForAndroid使用方法
KJFrameForAndroid的使用方法也是十分簡單,首先復制KJLibrary工程中bin目錄下的kjlibrary.jar文件至自己項目的libs文件夾中,然后在AndroidManifest.xml文件中添加以下權限規則:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
這樣就可以在Android項目中使用KJFrameForAndroid的所有功能了。
下面是利用KJFrameForAndroid實現的一些例子:
UILibrary
下面的代碼實現了一個Android Tab小工具
public class TabExample extends KJActivity {
@BindView(id = R.id.bottombar_content1, click = true)
public RadioButton mRbtn1;
@BindView(id = R.id.bottombar_content2, click = true)
private RadioButton mRbtn2;
@Override
public void setRootView() {
setContentView(R.layout.aty_tab_example);
}
@Override
protected void initWidget() {
super.initWidget();
mRbtn1.setText("widget clicked listener");
}
@Override
public void widgetClick(View v) {
super.widgetClick(v);
switch (v.getId()) {
case R.id.bottombar_content1:
ViewInject.toast("clicked mRbtn1");
break;
case R.id.bottombar_content2:
ViewInject.toast("clicked mRbtn2");
break;
}
}
} BitmapLibrary
下面的代碼實現了對Bitmap圖片的處理:
KJBitmap kjb = KJBitmap.create();
/**
* url can be local sdcard path or internet url;
* view can whichever View set image(for ImageView set src;for View set background).
*/
// local sdcard image
kjb.display(imageView, "file:///storage/sdcard0/1.jpg");
// internet url
kjb.display(textView, http://www.xxx.com/xxx.jpg);
//自定義圖片顯示大小
kjb.display(view, http://www.xxx.com/xxx.jpg, 80, 80); //width=80,height=80 HttpLibrary
下面的代碼實現了遠程獲取JSON的功能:
// get
kjh.get("http://www.oschina.net/", new HttpCallBack();//like post, so just one example
// post
KJHttp kjh = new KJHttp();
HttpParams params = new HttpParams();
params.put("id", "1");
params.put("name", "kymjs");
kjh.post("http://192.168.1.149/post.php", params, new HttpCallBack() {
@Override
public void onPreStart() {
super.onPreStart();
KJLoger.debug("before start");
}
@Override
public void onSuccess(String t) {
super.onSuccess(t);
ViewInject.longToast("request success");
KJLoger.debug("log:" + t.toString());
}
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
KJLoger.debug("exception:" + strMsg);
}
@Override
public void onFinish() {
super.onFinish();
KJLoger.debug("request finish. Regardless of success or failure.");
}
}); DBLibrary
下面的代碼實現了對數據庫的操作:
// data file
KJDB db = KJDB.create(this);
User ugc = new User(); //warn: The ugc must have id field or @ID annotate
ugc.setEmail("kymjs123@gmail.com");
ugc.setName("kymjs");
db.save(ugc);
//one - many
public class Parent{ //JavaBean
private int id;
@OneToMany(manyColumn = "parentId")
private OneToManyLazyLoader<Parent ,Child> children;
/*....*/
}
public class Child{ //JavaBean
private int id;
private String text;
@ManyToOne(column = "parentId")
private Parent parent;
/*....*/
}
List<Parent> all = db.findAll(Parent.class);
for( Parent item : all){
if(item.getChildren ().getList().size()>0)
Toast.makeText(this,item.getText() + item.getChildren().getList().get(0).getText(),Toast.LENGTH_LONG).show();
} 當然這些只是一些最簡單的例子,如果你熟悉Android開發,也可以去KJFrameForAndroid的官方網站上學習更多關于KJFrameForAndroid的高級用法。
本文鏈接:http://原網站已經失效/article/android-orm-kjframeforandroid.html
本文作者:碼農網 – 小峰