編輯:關於Android編程
package com.example.jekin.greendao.manager; import android.app.Application; import android.content.Context; import com.example.jekin.greendao.dao.DaoMaster; import com.example.jekin.greendao.dao.DaoSession; /** * Created by JeKin on 2016/4/8. */ public class App extends Application{ public static App mInstance; public static DaoMaster daoMaster; public static DaoSession daoSession; public static DaoManager daoManager; @Override public void onCreate() { super.onCreate(); mInstance = this; daoManager = DaoManager.getInstance(getApplicationContext()); } /** * 取得DaoMaster * * @param context * @return daoMaster */ public static DaoMaster getDaoMaster(Context context) { DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, "person.db", null); daoMaster = new DaoMaster(helper.getWritableDatabase()); return daoMaster; } /** * 取得DaoSession * * @param context * @return daoSession */ public static DaoSession getDaoSession(Context context) { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(context); } daoSession = daoMaster.newSession(); } return daoSession; } }DaoManager類:
package com.example.jekin.greendao.manager; import android.content.Context; import com.example.jekin.greendao.dao.DaoSession; import com.example.jekin.greendao.dao.Person; import com.example.jekin.greendao.dao.PersonDao; import java.util.List; import de.greenrobot.mydao.query.DeleteQuery; import de.greenrobot.mydao.query.QueryBuilder; import de.greenrobot.mydao.query.WhereCondition; /** * Created by JeKin on 2016/4/8. * 功能實現類 */ public class DaoManager { private static DaoManager instance; private static Context appContext; private DaoSession mDaoSession; private PersonDao personDao; public DaoManager(){ } public static DaoManager getInstance(Context context){ if (instance == null){ instance = new DaoManager(); if (appContext == null) { appContext = context.getApplicationContext(); } instance.mDaoSession = App.getDaoSession(context); instance.personDao = instance.mDaoSession.getPersonDao(); } return instance; } /** * ================Person====================* */ public Listutils文件夾下只有DaoUtils封裝類,易於後期擴展,懂點單詞的都很容易看懂orderAscPerson() { return personDao.queryBuilder().orderAsc(PersonDao.Properties.Id).list(); } /** * Person插入功能 * * @return * @param:album */ public void insertPerson(Person person) { personDao.insert(person); } public void insertOrReplacePerson(Person person) { personDao.insertOrReplaceInTx(person); } public void updatePerson(Person person) { personDao.update(person); } /** * Person查找功能 * //查找條件 * @param arg0 * @param conditions * @return:albumList */ public List queryPerson(WhereCondition arg0, WhereCondition... conditions) { QueryBuilder qb = personDao.queryBuilder(); qb.where(arg0, conditions); List personList = qb.list(); return personList; } /** * Person刪除所有功能 * * @param * @return */ public void deleteAllPerson() { personDao.deleteAll(); } /** * Person刪除功能 * * @return * @param:album */ public void deletePerson(Person person) { personDao.delete(person); } public void deletePersonByName(String name) { QueryBuilder qb = personDao.queryBuilder(); DeleteQuery bd = qb.where(PersonDao.Properties.Name.eq(name)) .buildDelete(); bd.executeDeleteWithoutDetachingEntities(); } }
package com.example.jekin.greendao.utils; import com.example.jekin.greendao.dao.Person; import com.example.jekin.greendao.dao.PersonDao; import com.example.jekin.greendao.dao.JsonCode; import com.example.jekin.greendao.manager.App; import java.util.List; /** * Created by JeKin on 2016/4/8. * 對數據庫的實現進行封裝,隱藏實現細節 */ public class DaoUtils { //===================getPersonDao======================== public static List最後就是我們的老大出場了,主要代碼就四句getPersonByName(String name) { List list = null; list = App.daoManager.queryPerson(PersonDao.Properties.Name.eq(name)); return list; } // 查找排序 public static List getPerson() { List personList = null; personList = App.daoManager.orderAscPerson(); return personList; } //==============================insertDao==================================== public static void insertPersonDao(JsonCode jsonCode) { // 添加數據 for (int i = 0; i < jsonCode.getPerson().size(); i++) { App.daoManager.insertPerson(jsonCode.getPerson().get(i)); } } public static boolean checkPersonExistAndUpdate(String name) { List personList = App.daoManager.queryPerson(PersonDao.Properties.Name.eq(name)); if (personList.size() > 0) { for (int i = 0; i < personList.size(); i++) { Person person = new Person(personList.get(i).getId(), personList.get(i).getName(), personList.get(i).getHigh(), personList.get(i).getAge()); App.daoManager.insertOrReplacePerson(person); } return true; } return false; } }
package com.example.jekin.greendao; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import com.example.jekin.greendao.dao.Person; import com.example.jekin.greendao.dao.JsonCode; import com.example.jekin.greendao.manager.App; import com.example.jekin.greendao.utils.DaoUtils; import com.google.gson.Gson; import java.util.ArrayList; /** * Created by JeKin on 2016/04/12 * 數據操作類 */ public class MainActivity extends AppCompatActivity { private static final String TAG1 = "addData"; private static final String TAG2 = "deleteData"; private static final String TAG3 = "changeData"; private static final String TAG4 = "queryData"; // 模擬JSON數據 private String jsonString = "{'code':'200','success':'true','Person':[{'name':'jekin','high':'173','age':'23'},{'name':'mike','high':'178','age':'24'}]}"; // 查找數據的條件 private String name = "mike"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JsonCode jsonCode = new Gson().fromJson(jsonString, JsonCode.class); // 添加數據 // 檢查是否已經存在該Person對象,不存在則插入 boolean isExist = DaoUtils.checkPersonExistAndUpdate(name); if (!isExist) { DaoUtils.insertPersonDao(jsonCode); } // 增加之後查找數據 for (Person person : DaoUtils.getPerson()) { Log.e(TAG1, person.getId().toString()); Log.e(TAG1, person.getName().toString()); Log.e(TAG1, person.getHigh().toString()); Log.e(TAG1, person.getAge().toString()); } // 條件刪除 App.daoManager.deletePersonByName(name); for (Person person : DaoUtils.getPerson()) { Log.e(TAG2, person.getId().toString()); Log.e(TAG2, person.getName().toString()); Log.e(TAG2, person.getHigh().toString()); Log.e(TAG2, person.getAge().toString()); } // 條件修改 if (DaoUtils.checkPersonExistAndUpdate(name)) { for (Person person : DaoUtils.getPersonByName(name)) { Log.e(TAG3, person.getId().toString()); Log.e(TAG3, person.getName().toString()); Log.e(TAG3, person.getHigh().toString()); Log.e(TAG3, person.getAge().toString()); } } // 修改之後查詢語句 for (Person person : DaoUtils.getPerson()) { Log.e(TAG4, person.getId().toString()); Log.e(TAG4, person.getName().toString()); Log.e(TAG4, person.getHigh().toString()); Log.e(TAG4, person.getAge().toString()); } } }
在開發中,我們經常使用到ListView這個控件。Android的API也提供了許多創建ListView適配器的快捷方式。例如ArrayAdapter、Sim
前面的兩篇博客寫了文件緩存,現在說說Android-Universal-Image-Loader的內存緩存,該內存緩存涉及到的類如圖所示 這些類的繼承關系如下圖所示:
鑒於谷歌最新推出的Android Studio備受開發者的推崇,所以也跟著體驗一下。一、介紹Android Studio Android Studio 是一個A
上一篇文章中我們介紹了android社區中比較火的熱修復功能,並介紹了目前的幾個比較流行的熱修復框架,以及各自的優缺點,同時也介紹了一下自身項目中對熱修復功能的實踐。目前